我的英文不好! 我已經嘗試了很多方法,但仍然出現錯誤:未找到頁面。 文件views.py:修復了django視圖的錯誤:找不到頁面
def create_article(request, template_name='vlogntruong/create_article.html'):
if request.method == "POST":
create_form = VlogForm(request.POST)
if create_form.is_valid():
new_article = create_form.save()
return HttpResponeRedirect(new_article.get_absolute_url())
else:
create_form = VlogForm(request)
template_context = {
'create_form': create_form,
}
return render_to_response(
template_name,
template_context,
RequestContext(request)
)
文件forms.py
from django import forms
from .models import VlogNTruong
class VlogForm(forms.Form):
title= forms.CharField(max_length=100,
widget=forms.TextInput(attrs={'size':50}))
name = forms.CharField(max_length=50,
widget = forms.TextInput(attrs={'size':50}))
sumary = forms.CharField(widget = forms.Textarea(attrs={"rows":10, "cols":80}))
content = forms.CharField(widget = forms.Textarea(attrs={"rows":20, "cols":80}))
video = forms.CharField(required = False,
widget = forms.TextInput(attrs={'size':60}))
def save(self):
create_form = VlogNTruong(title = self.cleaned_data['title'],
name = self.cleaned_data['name'],
sumary = self.cleaned_data['content'],
content = self.cleaned_data['content'],
video = self.cleaned_data['video'])
create_form.save()
return create_form
文件models.py:
from django.db import models
from django.db.models import permalink
from embed_video.fields import EmbedVideoField
from django.template.defaultfilters import slugify
from django.contrib import admin
import datetime
class VlogNTruong (models.Model):
title = models.CharField(max_length=100)
slug_field = models.TextField(null=True, blank=True)
name = models.CharField(max_length=50)
sumary = models.TextField()
content = models.TextField()
time_create = models.DateTimeField(auto_now_add=True)
video = EmbedVideoField(null=True, blank=True)
class Meta:
ordering = ('-time_create',)
def __unicode__(self):
return "%s" % self.title
@permalink
def get_absolute_url(self):
return ('vlogntruong.views.article_detail', [self.slug_field])
def save(self, *args, **kwargs):
if not self.slug_field:
self.slug_field = slugify(self.title)
super(VlogNTruong, self).save(*args, **kwargs)
文件HTML
{% extends 'vlogntruong/base.html' %}
{% block panel_content %}
<form action="/" method="post">{% csrf_token %}
<table>
{{form.as_table}}
</table>
<input type="submit" name="Create" />
</form>
{% endblock %}
文件URL
urlpatterns = patterns('vlogntruong.views',
url(r'^$', 'list_article',
{'template_name':'vlogntruong/list_article.html'}),
url(r'^(?P<slug_field>[-\w\d]+)/$', 'article_detail'),
url(r'^create/$', 'create_article'),
)
幫助我
錯誤
Page not found (404)
Request Method: GET
Request URL: /nhuttruong/create/
No VlogNTruong matches the given query.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
你從哪裏得到這個錯誤嗎?哪一部分? –
錯誤消息:找不到頁面。沒有其他錯誤 – dotiep0402
您的視圖中沒有任何與您的urls.py中的方法相匹配的方法,這就是爲什麼django找不到任何東西。你有'list_article','article_detail'和'create',但是在你的views.py中有'create_article',它甚至沒有被映射。 –