当导航到 django 中的下一个 link 时,Slug URL 变成一个数字
Slug URL turns into a number when navigating to the next link in django
我试着在这里搜索这个问题,但我没有找到任何东西,我希望有人可以帮助回答或发送 link 到 Whosebug 上的解决方案...
我一直在创建一个博客,当我点击 post 标题时,它会转到 'post_details.html' 并在 URL 中显示带有 post 标题。喜欢这里:
examplename.com/blog/brooklyn-nets-at-miami-heat-preview/
但是当我点击在 post 上写评论时,它会将我带到一个表单页面,但是 slug 标题消失了,它只显示 post 数字。像这样:
examplename.com/blog/3/add-comment
有人可以解释为什么会这样吗?我想知道如何解决这个问题,并了解出现的这个数字是从哪里来的。
Post_detail.html
{% block content %}
<h2 class="title">
<a class="post-title" href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Publicado em {{ post.created }} por {{ post.author }}
</p>
{{ post.body|linebreaks }}
<hr>
<h3>Comments</h3>
{% if not post.comments.all %}
Nobody commented yet...<a href="{% url 'blog:commentview' post.pk %}">Add a comment</a>
{% else %}
<a href="{% url 'blog:commentview' post.pk %}">Add a comment</a>
{% for comment in post.comments.all %}
{{ comment.name }}
{{ comment.date }}
<br>
{{ comment.body }}
{% endfor %}
{% endif %}
<a class="swen" href="{% url 'blog:list' %}">More Articles</a>
{% endblock %}
urls.py
from unicodedata import name
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path("", views.PostListView.as_view(), name="list"),
path("<slug:slug>/", views.PostDetailView.as_view(), name="detail"),
path("<slug:slug>/add-comment", views.CommentView.as_view(), name="commentview")
]
models.py
from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse ('blog:detail', kwargs={"slug":self.slug})
class Meta:
ordering = ("-created",)
class Comment(models.Model):
post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
name = models.CharField(max_length=255)
body = models.TextField()
date = models.DateField(auto_now_add=True)
slug = Post.slug
def __str__(self):
return reverse (self.post.title, self.name, self.post.slug)
views.py
from xml.etree.ElementTree import Comment
from django.views.generic import DetailView, ListView, CreateView
from .models import Post, Comment
class PostListView(ListView):
model = Post
class PostDetailView(DetailView):
model = Post
class CommentView(CreateView):
model = Comment
template_name = 'add_comment.html'
fields = '__all__'
您传递的 pk 是一个整数类型作为参数,而不是路径名 commentview 的 slug。也许你应该检查一下。
我试着在这里搜索这个问题,但我没有找到任何东西,我希望有人可以帮助回答或发送 link 到 Whosebug 上的解决方案...
我一直在创建一个博客,当我点击 post 标题时,它会转到 'post_details.html' 并在 URL 中显示带有 post 标题。喜欢这里:
examplename.com/blog/brooklyn-nets-at-miami-heat-preview/
但是当我点击在 post 上写评论时,它会将我带到一个表单页面,但是 slug 标题消失了,它只显示 post 数字。像这样:
examplename.com/blog/3/add-comment
有人可以解释为什么会这样吗?我想知道如何解决这个问题,并了解出现的这个数字是从哪里来的。
Post_detail.html
{% block content %}
<h2 class="title">
<a class="post-title" href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Publicado em {{ post.created }} por {{ post.author }}
</p>
{{ post.body|linebreaks }}
<hr>
<h3>Comments</h3>
{% if not post.comments.all %}
Nobody commented yet...<a href="{% url 'blog:commentview' post.pk %}">Add a comment</a>
{% else %}
<a href="{% url 'blog:commentview' post.pk %}">Add a comment</a>
{% for comment in post.comments.all %}
{{ comment.name }}
{{ comment.date }}
<br>
{{ comment.body }}
{% endfor %}
{% endif %}
<a class="swen" href="{% url 'blog:list' %}">More Articles</a>
{% endblock %}
urls.py
from unicodedata import name
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path("", views.PostListView.as_view(), name="list"),
path("<slug:slug>/", views.PostDetailView.as_view(), name="detail"),
path("<slug:slug>/add-comment", views.CommentView.as_view(), name="commentview")
]
models.py
from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse ('blog:detail', kwargs={"slug":self.slug})
class Meta:
ordering = ("-created",)
class Comment(models.Model):
post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
name = models.CharField(max_length=255)
body = models.TextField()
date = models.DateField(auto_now_add=True)
slug = Post.slug
def __str__(self):
return reverse (self.post.title, self.name, self.post.slug)
views.py
from xml.etree.ElementTree import Comment
from django.views.generic import DetailView, ListView, CreateView
from .models import Post, Comment
class PostListView(ListView):
model = Post
class PostDetailView(DetailView):
model = Post
class CommentView(CreateView):
model = Comment
template_name = 'add_comment.html'
fields = '__all__'
您传递的 pk 是一个整数类型作为参数,而不是路径名 commentview 的 slug。也许你应该检查一下。