首先,我肯定知道我有一个与给定查询匹配的项目,所以我不明白这个错误。开始了。
我在urls.py中有这两种模式,一种用于创建,另一种用于版本:
urlpatterns = patterns('',
url(r'^(?P<project_id>[A-Z0-9_a-z]+)/add_test_setup/$', add_test_setup, name="add_test_setup"),
url(r'^(?P<project_id>[0-9]+)/testSetup/(?P<testsetup_id>\w+)/edit/$', add_test_setup, name="edit_test_setup"),
)
这是等效的观点:
def add_test_setup(request, project_id, testsetup_id=None):
project = get_object_or_404(Project, pk=int(project_id))
if testsetup_id != None:
testsetup = get_object_or_404(TestSetup, pk=int(testsetup_id))
testsetupform = TestSetupForm(request.POST or None, instance=testsetup)
if request.method == "POST" and testsetupform.is_valid():
testsetupform.save()
return redirect("/projects/"+testsetup.project.ref+"/info/#tabr2", testsetup.project.ref)
else:
return render_to_response('projects/edit_test_setup.html', {'testsetupform':testsetupform,'project':project}, context_instance=RequestContext(request))
else:
testsetupform = TestSetupForm(request.POST or None)
if request.method == "POST" and testsetupform.is_valid():
testSetup.save()
return redirect("/projects/"+project.ref+"/info/#tabr2", project.ref)
else:
return render_to_response('projects/add_test_setup.html', {'testsetupform':testsetupform,'project':project}, context_instance=RequestContext(request))
这些是模型:
class Project(models.Model):
STATUS_CHOICES = ((0,'Not started'),(1,'ZERO'),(2,'KOM'))
ref = models.CharField(max_length=70, unique=True)
description = models.TextField(_("Description"),max_length=170, blank=True)
notes = models.TextField(max_length=170,blank=True)
customer = models.CharField(max_length=70)
operator = models.ManyToManyField(User)
supervisor = models.CharField(max_length=70, blank=True)
start_date = models.DateField(blank=True,null=True)
finish_date = models.DateTimeField(blank=True, null=True)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default=STATUS_CHOICES[0][1])
goal = models.TextField(max_length=170, blank=True)
class TestSetup(models.Model):
ref = models.CharField(max_length=30)
description = models.TextField(max_length=200,null=True,blank=True)
notes = models.TextField(max_length=200,null=True,blank=True)
project = models.ForeignKey(Project)
def __unicode__(self):
return "Test set-up %s" % (self.ref)
我添加新的TestSetup没有任何问题,但是当我编辑TestSetup时,它会抛出404&#34; No Project匹配给定的查询&#34;。起初,我认为问题是视图的get_object_or_404
,但后来我对此代码进行了评论,并将其替换为Project.objects.get(id=int(project_id))
。没啥事儿。
这是我执行testsetupform版本的HTML代码的一部分:
<form class="vertical" method="post" action="{% url 'edit_test_setup' testsetupform.instance.project.id testsetupform.instance.id %}"> {% csrf_token %}
{{testsetupform}}
<h6 style="margin-top:30px">Assign series <span class="tooltip" title="Assign series to this test set-up."><i class="fa fa-question-circle" style="cursor:pointer;color:green;"></i></span></h6>
<hr class="alt1" />
<input type="submit" class="button blue" value="Save" style="margin-top:30px"></input>
</form>
此外,在404页面中,显示:
Request Method: POST
Request URL: http://127.0.0.1:8000/projects/5/testSetup/16/edit/
因此,Project(id = 5)和TestSetup(id = 16)模型都存在!我已经使用其他Project模型测试并抛出相同的错误。
----------- EDITION.1 ------------
此外,使用./manage.py shell
:
>>> from django.shortcuts import get_object_or_404
>>> from projects.models import Project
>>>
>>> project = get_object_or_404(Project, pk=5)
>>> project
<Project: 182TRE>
>>> Project.objects.get(id=5)
<Project: 182TRE>
>>>
----------- END OF EDITION.1 ------------
哪里可能是问题?
----------- EDITION.2 ------------
我在HTML表单中使用render_to_string
快捷方式。编辑TestSetup时会调用此快捷方式。我所拥有的是带有DIV的主模板,我在其中加载了一个额外的模板来编辑这些TestSetup。这是我用来请求此模板的AJAX调用:
function loadTestSetup(setup_id){
$.ajax({
type: "GET",
url : '/projects/testSetup/'+ setup_id + '/load/',
data: "",
dataType: 'text',
success : function (data, textStatus, request) {
data = eval("(" + data + ")");
$("#testsetupedit-dialog").html(data.html) // "testsetupedit-dialog" is a div of the original template.
}
});
}
此AJAX调用通过此视图请求模板:
def load_test_setup(request, setup_id):
testSetup = get_object_or_404(TestSetup, pk=int(setup_id))
setupform = TestSetupForm(None, instance=testSetup)
data = {'html':""}
data['html'] = render_to_string('projects/test_setup_form.html', {'setupform':setupform}, context_instance=RequestContext(request))
return HttpResponse(simplejson.dumps(data))
使用此网址格式:
url(r'^testSetup/(?P<setup_id>\w+)/load/$', load_test_setup, name="load_test_setup"),
我可能会对render_to_string
使用不当吗?
----------- END OF EDITION.2 ------------
答案 0 :(得分:0)
重定向后收到404错误。 这一行:
return redirect("/projects/"+testsetup.project.ref+"/info/#tabr2", testsetup.project.ref)
或者这一行:
return redirect("/projects/"+project.ref+"/info/#tabr2", project.ref)
将您重定向到错误的网址,然后获得404。
建议:
django.core.urlresolvers.reverse
生成重定向网址。如果未注册URL,它将引发更有意义的异常。