我知道HttpResponseRedirect只接受一个参数,一个URL.但有些情况下我想重定向并显示错误消息.
我正在阅读这篇文章:如何使用HTTP重定向(在Django中)传递信息,并且有很多好的建议.我真的不想使用我不知道如何工作的库.我不想依赖于根据Django文档将被删除的消息.我想过使用会话.我也喜欢在URL中传递它的想法,例如:
return HttpResponseRedirect('/someurl/?error=1')
Run Code Online (Sandbox Code Playgroud)
然后有一些从错误代码到消息的映射.拥有类似于全局地图的结构是不错的做法,这些结构在这些错误消息中是硬代码还是有更好的方法?
或者我应该只使用会话
编辑:我使用会话工作.将这样的事情放在会话中是一种很好的做法吗?
我正在通过一个简单的Objective-C/Cocoa程序来尝试学习该语言,并且对于从我编写的代码到界面构建器的某些内容的链接方式感到有些困惑.
例如,我有一个简单的NSString:
@property (assign) NSString *letters;
Run Code Online (Sandbox Code Playgroud)
在我的界面构建器中,我有一个文本字段,我使用文本字段的绑定将它连接到字母.
但是,这个例子也有:
@property (assign) IBOutlet NSArrayController *wordsController;
Run Code Online (Sandbox Code Playgroud)
在视图中,我有一个表不断变化并显示不同的单词,这些单词存储在NSMutableArray中.我想我可以理解我无法将数组绑定到表中,因为有一些更复杂的问题.因此,在Interface Builder中,我创建了一个Array Controller并将其绑定到表中.在Array Controller的绑定中,我将Array Controller绑定到单词数组.
我知道我要做的最后一件事也是将Array Controller绑定到我的NSArrayController对象.我不明白为什么我通过主控制器对象通过在这个插座和wordsController之间建立连接来做到这一点.在Array Controller的绑定部分中,有一个灰色的选项Content Object,它表示"NSArrayController将NSArrayController视为其内容." 为什么我不在这里设置绑定?它作为一个出口有什么意义,为什么它与我的NSString字母有什么不同?
谢谢
这需要我很长时间才能制作出这种简单的形式.几乎在那里,但是当我提交时,我得到NoneType错误
views.py:
from djangoproject1.authentication import forms
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
def main(request):
rf = forms.RegisterForm()
pf = forms.ProfileForm()
return render_to_response("authentication/index.html", {'form1': rf, 'form2':pf})
def register(request):
if request.method == 'POST':
rf = forms.RegisterForm(request.POST)
pf = forms.ProfileForm(request.POST)
if rf.is_valid() and pf.is_valid():
newuser = User(username=rf.cleaned_data['username'],email=rf.cleaned_data['email']) # this is the offending line
newuser.set_password(rf.cleaned_data['password'])
newuser.save()
profile = pf.save(commit=False)
profile.user = newuser
profile.save()
return HttpResponseRedirect("/register-success/")
else:
return main(request)
Run Code Online (Sandbox Code Playgroud)
forms.py:
from django import forms
from djangoproject1.authentication.models import UserProfile
class …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用带有月份列表的选择表单,但我似乎无法正确发布它.
形成:
class MonthForm(forms.Form):
months = [('January','January'),
('February','February'),
('March','March'),
('April','April'),
('May','May'),
('June','June'),
('July','July'),
('August','August'),
('September','September'),
('October','October'),
('November','November'),
('December','December'),]
month = forms.ChoiceField(months)
Run Code Online (Sandbox Code Playgroud)
视图:
def table_view(request):
if request.method == 'POST':
month = MonthForm(request.POST).cleaned_data['month']
print month
Run Code Online (Sandbox Code Playgroud)
我一直收到这个错误:
'MonthForm' object has no attribute 'cleaned_data'
Run Code Online (Sandbox Code Playgroud) 我试图在python中找到关于反射的一些信息.我发现了一篇维基百科文章,它将此作为代码片段:
# without reflection
Foo().hello()
# with reflection
getattr(globals()['Foo'](), 'hello')()
Run Code Online (Sandbox Code Playgroud)
我无法让这个工作.我真正需要的是一种实例化对象的方法.所以,如果我有一个字符串'Foo',我希望能够得到一个Foo类型的对象.就像在java中我可以这样说:Class.forName("Foo")
刚刚发现这个......想知道为什么我以前找不到这个: python是否具有Java Class.forName()的等价物?
我知道你不能使用过滤器来调用方法,因为它是基于数据库来实现的.所以我正在尝试编写自定义过滤器.
@staticmethod
def custom_filter(obj,method_name, arg=False):
for i in obj.objects.all():
if getattr(i, method_name)() == arg:
yield i
Run Code Online (Sandbox Code Playgroud)
如果我打电话,我可以让它工作:
MyModel.custom_filter(MyModel,'my_method','myarg')
Run Code Online (Sandbox Code Playgroud)
但是,我不能像常规过滤器那样将它们链接在一起.
我可以这样做:
@staticmethod
def custom_double_filter(obj,method1,arg1,method2,arg2):
for i in obj.objects.all():
if getattr(i, method1)() == arg1 and getattr(i,method2)()==arg2:
yield i
Run Code Online (Sandbox Code Playgroud)
但我希望它能支持任意数量的过滤器,而且有些方法有自己的参数
@staticmethod
def custom_double_filter(obj,method1,arg1,method2,arg2):
for i in obj.objects.all():
if getattr(i, method1)(<may need to pass an argument>) == arg1 and getattr(i,method2)()==arg2:
yield i
Run Code Online (Sandbox Code Playgroud)
编辑:所以我试着像这样制作一个自定义管理器:
class GroupManager(models.Manager):
use_for_related_fields = True
def custom_filter(self,method_name, arg=False):
results = []
for i in self.all():
if getattr(i, method_name)() == arg:
results.append(i) …Run Code Online (Sandbox Code Playgroud) 我有一个NSTextField,里面有一个NSNumberFormatter.我已经看过文本字段,如果你把它们留空,只需将之前的数字放入其中.我很好奇Interface Interface中是否有一个提供此行为的设置.我似乎无法找到它,但我对IB很新,可能不会找到合适的位置.
谢谢
cocoa objective-c interface-builder nstextfield nsnumberformatter
我一直在研究在表格中设置动画行NSTableView的moveRowAtIndex:toIndex方法.根据我的判断,排序并没有多大帮助.我对它是如何工作的解释是,如果我想将第0行移动到第4行,那么它们之间的行将被适当地处理.但是,如果我有一个带有支持它的数组的表视图,然后我对数组进行排序,我希望表视图从旧状态动画到新状态.我不知道哪些项目是移动的项目与那些移动以适应移动项目的项目.
例:
[A,B,C,D] --> [B,C,D,A]
Run Code Online (Sandbox Code Playgroud)
我知道第0行移到了第3行,所以我想说[tableView moveRowAtIndex:0 toIndex:3].但是如果我对[A,B,C,D]应用一些自定义排序操作使其看起来像[B,C,D,A],我实际上并不知道第0行移动到第3行而不是第1行,2和3移动到行0,1和2.我认为我应该能够指定所有的移动(第0行移动到第4行,第1行移动到第0行等)但是当我尝试时,动画看起来不正确.
有一个更好的方法吗?
编辑:我发现这个网站,似乎做我想要的,但似乎有点多的东西应该是简单的(至少我认为它应该是简单的)
我试图在php中使用"get"传递变量,但遇到了障碍.
这是我的PHP文件:
<?php
include '../includes/header.php';
?>
<div id="page">
<div id="content">
<h3><?php $_GET['head']; ?></h3>
<div id="screenshots"> <img src="../images/sites/<?php $_GET['img1']; ?>" /> <img src="../images/sites/<?php $_GET['img2']; ?>" /> </div>
<div id="description">
<p><?php $_GET['p1']; ?></p>
<p><?php $_GET['p2']; ?></p>
</div>
</div>
</div>
<?php
include '../includes/footer.php';
?>
Run Code Online (Sandbox Code Playgroud)
为了测试它,我做了一个简单的请求:
<a href="work/test.php?head=a&img1=b&img2=c&p1=d&p2=e"><img src="images/sites/thumbs/thumb.jpg"/></a>
Run Code Online (Sandbox Code Playgroud)
它转到正确的页面,但没有看到任何变量.我在某个地方犯了一个愚蠢的错误吗?谢谢!
我正在使用pydev在Eclipse中使用Django.我正在遵循这里的教程(http://docs.djangoproject.com/en/dev/intro/tutorial01/),制作一个简单的民意调查应用程序.在他们运行shell的示例中,他们可以这样做:
from polls.models import Poll, Choice
Run Code Online (Sandbox Code Playgroud)
但是,出于某种原因,我无法做到这一点.为了使它工作,我必须这样做:
from projectname.polls.models import Poll, Choice
Run Code Online (Sandbox Code Playgroud)
知道为什么会这样吗?这是日食吗?我的settings.py中的某个路径有问题吗?谢谢!
我有一个网络应用程序,用户可以在其中拥有个人资料(有点像Facebook),他们可以查看自己的个人资料以及其他人的个人资料.您在自己的个人资料中看到的内容就是一切,但查看您个人资料的其他人可能无法看到其中的所有内容.
为了实现这一目标,我有common-profile.html和profile.html,其中profile.html包含common-profile.html,common-profile.html是每个人都可以看到的.因此,如果我想查看自己的个人资料,我会看到profile.html,但其他人会看到common-profile.html.
问题是当我使用模板继承时,这两个模板都从一些基本模板继承,因此模板会被导入两次.
profile.html:
{% extends 'base.html' %}
{% block content %}
{% include 'common-profile.html' %}
...other stuff would go here
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
共profile.html:
{% extends 'base.html' %}
{% block content %}
<h1>{{c_user.first_name}} {{c_user.last_name}}<h1>
...other stuff would go here
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
这只是一个坏主意吗?我应该只有一个配置文件并检查权限/在模板标签中使用一些if语句吗?我不希望在我的html页面中有太多的逻辑,但如果它只是一些if语句来决定要显示什么,也许那没关系?
我有一个Foo实现+(void)initialize方法的类.我也有一个类的子类Foo.当我实例化子类时,该initialize方法也被调用Foo,我不想要.我该如何防止这种情况?
谢谢.
除了我释放爬虫时的工作:
public void setCrawlerFree(WebCrawler w)
{
synchronized(myFreeCrawlers)
{
synchronized(numToGo)
{
myFreeCrawlers.add(w);
myFreeCrawlers.notifyAll();
numToGo--;
numToGo.notify();
}
}
}
Run Code Online (Sandbox Code Playgroud)
爬虫完成后,我可以将其添加回列表中.我还想从我仍然需要做的事情中减去1.我有一个主线程等待,直到numToGo为0.我在numToGo.notify()上得到一个IllegalMonitorStateException,但由于它在同步块内,这是不是意味着我拥有它?
django ×7
python ×6
cocoa ×4
objective-c ×4
concurrency ×1
django-forms ×1
eclipse ×1
get ×1
html ×1
iboutlet ×1
java ×1
nstableview ×1
nstextfield ×1
php ×1
pydev ×1
reflection ×1