2016-10-01 18 views
0

時々、私は約ページ、連絡先、作成またはブログを開こうとしています このエラーが発生します:raising by:blog.views.blog_postDetailView 私donnt理由を知っているが、私はそれが私のurlpattterns またはそれらの順序でsthをしなければならないと思う。ここに私のコード。 はあなたの助けと改善 おかげでたくさんDjangoページが見つかりません404、投稿者:blog.views.blog_postDetailView

mainurl

from django.conf.urls import url, include 
from django.contrib import admin 


from django.conf import settings 
from django.conf.urls.static import static 

from blog.views import AboutPageView, ContactPageView, blog_postCreateView 

urlpatterns = [ 

    url(r'^about/', AboutPageView.as_view(), name='about'), 
    url(r'^contact/', ContactPageView.as_view(), name='contact'), 
    url(r'^create/', blog_postCreateView.as_view(), name='blog_post_create'), 
    url(r'', include('blog.urls')), 
    url(r'^blog/', include('blog.urls')), 

    #admin and login 
    url(r'^admin/', admin.site.urls), 
    url(r'^accounts/', include('registration.backends.default.urls')), 

] 


if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

ブログのURL

from django.conf.urls import url 

from .views import blog_postListView, blog_postDetailView, blog_postCreateView 

urlpatterns = [ 

    url(r'^$', blog_postListView.as_view(), name='blog_post_list'), 
    url(r'^(?P<slug>[-\w]+)$', blog_postDetailView.as_view(), name='blog_post_detail'), 
] 

ベースHTML、NAVの一部

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
     <ul class="nav navbar-nav"> 
       <li> 
       <a href="/">Home</a> 
      </li> 
      <li> 
       <a href="{% url 'about' %}">About</a> 
      </li> 
      <li> 
       <a href="{% url 'contact' %}">Contact</a> 
      </li> 
      <li> 
       <a href="/blog">Blog</a> 
      </li> 
      <li> 
      {% if request.user.is_authenticated %} 
        <li><a href="{% url 'auth_logout' %}">Logout</a></li> 
       {% else %} 
       <li><a href="{% url 'auth_login' %}">Login</a></li> 
       <li><a href="{% url 'registration_register' %}">Register</a></li> 
      {% endif %} 

モデル

に感謝しますの
rom __future__ import unicode_literals 

from django.conf import settings 
from django.core.urlresolvers import reverse 
from django.db import models 

from django.db.models.signals import pre_save, post_save 
from django.utils.text import slugify 
# Create your models here. 





def upload_location(instance, filename): 
    #filebase, extension = filename.split(".") 
    #return "%s/%s.%s" %(instance.id, instance.id, extension) 
    return "%s/%s" %(instance.id, filename) 


class blog_post(models.Model): 
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) 
    title = models.CharField(max_length=120) 
    height_field = models.IntegerField(default=0) 
    width_field = models.IntegerField(default=0) 
    image = models.ImageField(upload_to=upload_location, null=True, blank=True, width_field="width_field", height_field="height_field") 
    #height_field = models.PositiveIntegerField() 
    #width_field = models.PositiveIntegerField() 
    slug = models.SlugField(unique=True) 
    content = models.TextField() 
    updated = models.DateTimeField(auto_now=True, auto_now_add=False) 
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True) 

    def __unicode__(self): 
     return self.title 

    def __str__(self): 
     return self.title 

    def get_absolute_url(self): 
     return reverse("blog_post_detail", kwargs={"slug": self.slug}) 


def create_slug(instance, new_slug=None): 
    slug = slugify(instance.title) 
    if new_slug is not None: 
     slug = new_slug 
    qs = blog_post.objects.filter(slug=slug).order_by("-id") 
    exists = qs.exists() 
    if exists: 
     new_slug = "%s-%s" %(slug, qs.first().id) 
     return create_slug(instance, new_slug=new_slug) 
    return slug 


def pre_save_post_receiver(sender, instance, *args, **kwargs): 
    if not instance.slug: 
     instance.slug = create_slug(instance) 



pre_save.connect(pre_save_post_receiver, sender=blog_post) 

ビュー

from django.contrib.auth.decorators import login_required 
from django.core.urlresolvers import reverse 
from django.http import HttpResponse 
from django.views.generic import View 
from django.views.generic.base import TemplateView, TemplateResponseMixin, ContextMixin 
from django.views.generic.detail import DetailView 
from django.views.generic.list import ListView 
from django.views.generic.edit import CreateView, UpdateView, DeleteView 
from django.shortcuts import render 
from django.utils.decorators import method_decorator 




from .models import blog_post 
from .forms import blog_postForm 
# Create your views here. 


class LoginRequiredMixin(object): 
    @classmethod 
    def as_view(cls, **kwargs): 
     view = super(LoginRequiredMixin, cls).as_view(**kwargs) 
     return login_required(view) 

    #@method_decorator(login_required) 
    #def dispatch(self, request, *args, **kwargs): 
    # return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs) 



class blog_postCreateView(LoginRequiredMixin, CreateView): 
    #model = blog_post 
    form_class = blog_postForm 
    template_name = "form.html" 
    #fields = ["title", "content"] 
    def get_success_url(self): 
     return reverse("blog_post_list") 

    # @method_decorator(login_required) 
    # def dispatch(self, request, *args, **kwargs): 
    # return super(MyView, self).dispatch(request, *args, **kwargs)  


class blog_postListView(ListView): 

    model = blog_post 
    def get_queryset(self, *args, **kwargs): 
     qs = super(blog_postListView, self).get_queryset(*args, **kwargs).order_by("-timestamp") 
     return qs 

class blog_postDetailView(DetailView): 

    model = blog_post 

class AboutPageView(TemplateView): 

    template_name = "about.html" 


class ContactPageView(TemplateView): 

    template_name = "contact.html" 

更新のURL

from django.conf.urls import url, include 
from django.contrib import admin 


from django.conf import settings 
from django.conf.urls.static import static 

from blog.views import AboutPageView, ContactPageView, blog_postCreateView 

urlpatterns = [ 

    url(r'^about/', AboutPageView.as_view(), name='about'), 
    url(r'^contact/', ContactPageView.as_view(), name='contact'), 
    url(r'^create/', blog_postCreateView.as_view(), name='blog_post_create'), 
    url(r'^categories/', include('blog.urls_categories')), 
    #url(r'', include('blog.urls')), 
    url(r'^blog/', include('blog.urls')), 

    #admin and login 
    url(r'^admin/', admin.site.urls), 
    url(r'^accounts/', include('registration.backends.default.urls')), 
    url(r'', include('blog.urls')), 

] 


if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

答えて

0

urlpatterns = [ 
    #all other urls, 
    url(r'', include('blog.urls')), 
] 

OR

のよう url_patternsリストの最後に以下の行を入れて
url(r'^blogdetail/', include('blog.urls')), 
+0

やあ、あなたの助けに感謝しますが、 すべてのページが作成し、「ブログ」ページを除いて、仕事、 あなたはとても親切にしてください可能性があり、それは、その後、順序または代わりのブログを変更する 原因を動作するはずです理由を説明blogdetailを使うと、ブログリンクのテンプレートタグは何でしょうか?ありがとうalot –

+0

あなたの更新されたurls.pyを投稿してください。 – itzMEonTV

+0

ありがとう、この注文のように私のためにうまく動作します:) 上記の更新されたURLを投稿し、なぜそれが動作する理由を説明することができます 私は最後にURLを入れて? –

関連する問題