Django:无法重定向

时间:2019-03-26 09:05:31

标签: python django

我根据纪录片在Django上创建了一个应用。我创建了基本HTML,然后分别创建了2个,列表和详细信息。 list.html可以正常工作,但是当我单击某个帖子时,它会重定向到同一页面

我尝试订购URL,但这不起作用。

models.py


class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager, self)\
            .get_queryset()\
            .filter(status='published')


class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250,
                            unique_for_date='publish')
    author = models.ForeignKey(User, related_name='blog_posts', on_delete=models.CASCADE)
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, 
    default='draft')

    objects = models.Manager()
    published = PublishedManager()


    class Meta:
        ordering = ('-publish',)

    def __str__(self):
        return self.title


    def get_absolute_url(self):
    return reverse('blog:post_detail', args=[self.publish.year,
                                             self.publish.strftime('%m'),
                                             self.publish.strftime('%d'),
                                             self.slug])




该应用程序的urls.py

urlpatterns = [

url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/'\
    r'(?P<post>[-\w]+)/$',
    views.post_detail,
    name='post_detail'),
url(r'^$', views.post_list, name='post_list'),
]
views.py

    def post_list(request):
    posts = Post.published.all()
    return render(request, 'blog/post/list.html', {'posts': posts})


    def post_detail(request, year, month, day, post):
    post = get_object_or_404(Post, slug=post, status='published',
                             publish__year=year,
                             publish__month=month,
                             publish__day=day)
    return render(request,
                  'blog/post/detail.html',
                  {'post': post})


主要(mysite)的urls.py

app_name = 'blog'
urlpatterns = [
url(r'^blog/', include('blog.urls')),
url(r'^admin/', admin.site.urls),

]
base.html

    {% load staticfiles %}
    <!DOCTYPE html>
    <html>
    <head>
    <title>{% block title %}{% endblock %}</title>

    </head>
    <body>
    <div id="content">
    {% block content %}
    {% endblock %}
    </div>
    <div id="sidebar">
    <h2>My blog</h2>
      <p>This is my blog.</p>
    </div>
    </body>
    </html>


list.html

{% extends "blog/base.html" %}

{% block title %}My Blog{% endblock %}

{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
  <a href="{{ post.get_absolute_url }} " target="_blank">
    {{ post.title }}
  </a>
</h2>
<p class="date">
  Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
detail.html

    {% extends "blog/base.html" %}

    {% block title %}My Blog{% endblock %}

    {% block content %}
      <h1>My Blog</h1>
      {% for post in posts %}
      <h2>
      <a href="{{ post.get_absolute_url }} " target="_blank">
        {{ post.title }}
      </a>
      </h2>
      <p class="date">
      Published {{ post.publish }} by {{ post.author }}
      </p>
      {{ post.body|truncatewords:30|linebreaks }}
      {% endfor %}
      {% endblock %}

1 个答案:

答案 0 :(得分:0)

您将app_name声明放在错误的urls.py中。它需要进入应用程序内部的一个,而不是项目级别的一个。