2014-06-07 49 views
0

我的英文不好! 我已經嘗試了很多方法,但仍然出現錯誤:未找到頁面。 文件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. 
+0

你從哪裏得到這個錯誤嗎?哪一部分? –

+0

錯誤消息:找不到頁面。沒有其他錯誤 – dotiep0402

+0

您的視圖中沒有任何與您的urls.py中的方法相匹配的方法,這就是爲什麼django找不到任何東西。你有'list_article','article_detail'和'create',但是在你的views.py中有'create_article',它甚至沒有被映射。 –

回答

0

你的問題是,你在你的形式save()方法沒有返回它在數據庫中創建對象,它返回的未保存實例,您正在嘗試獲取重定向url。

我建議你閱讀modelforms guide來幫助你。

你的形式應該是這樣的:

from django import forms 
from .models import VlogNTruong 

class VlogForm(forms.ModelForm): 
    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})) 

    class Meta: 
     model = VlogNTruong 

然後,在你的意見,你需要調整這樣的:

from django.shortcuts import render 

def create_article(request, template_name='vlogntruong/create_article.html'): 
    create_form = VlogForm(request.POST or None) 
    if create_form.is_valid(): 
     new_article = create_form.save() 
     return HttpResponeRedirect(new_article.get_absolute_url()) 
    template_context = {'create_form': create_form,} 
    return render(request, template_name, template_context) 

您還需要確保該VlogNTruong模型一個get_absolute_url method它返回一個URL。你的方法只是返回一個元組,解決這個問題是這樣的:

def get_absolute_url(self): 
    from django.core.urlresolvers import reverse 
    return reverse('vlogntruong.views.article_detail', args=[self.slug_field]) 

最後你__unicode__方法必須返回一個Unicode對象,你返回一個字符串:

def __unicode__(self): 
    return unicode('{}'.format(self.title)) 
0

您正在使用第三URL()不與請求的URL匹配的正則表達式:

R'^創建/ $'將不匹配'/ nhuttruong/create /'

爲了達到你的目標,你應該使用類似R「 + /創建/ $」

+0

文件夾應用程序的urls.py,urls.py main:url(r'^ nhuttruong /',include('vlogntruong.urls')), – dotiep0402