反向 'post/profile/' 与参数 '()' 和关键字参数 '{'user': 1}' 未找到。尝试了 0 种模式:[]
Reverse for 'post/profile/' with arguments '()' and keyword arguments '{'user': 1}' not found. 0 pattern(s) tried: []
我的 Django 项目有问题。当我想显示属于特定用户 ID 的帖子时,这会向我显示此错误
urls.py:
url(r'^profile/(?P<user>\d+)/$', profile)
views.py:
def profile(request, user=None):
queryset = Post.objects.filter(user=user)
context ={
"object_list" : queryset,
}
return render(request, 'profile.html', context)
html 页数:
{% if request.user.is_authenticated %}
<li><a href=" {% url 'post/profile/' user=request.user.id %} ">My ADS</a></li>
models.py:
class Post(models.Model):
"""docstring for Post"""
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
post_title = models.CharField(max_length=50)
image = models.ImageField(upload_to = 'user/',null=True, blank=True, height_field="height_field", width_field="width_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
content = models.TextField()
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=True, auto_now_add=False)
def __str__(self):
return (self.post_title)
def get_absolute_url(self):
return reverse("posts:detailpost", kwargs={"id": self.id})
{% url %}
标签采用 URL 模式的名称,而不是路径。您应该为模式命名:
url(r'^profile/(?P<user>\d+)/$', profile, name='profile')
并像这样使用标签:
{% url 'profile' user=request.user.id %}
我的 Django 项目有问题。当我想显示属于特定用户 ID 的帖子时,这会向我显示此错误
urls.py:
url(r'^profile/(?P<user>\d+)/$', profile)
views.py:
def profile(request, user=None):
queryset = Post.objects.filter(user=user)
context ={
"object_list" : queryset,
}
return render(request, 'profile.html', context)
html 页数:
{% if request.user.is_authenticated %}
<li><a href=" {% url 'post/profile/' user=request.user.id %} ">My ADS</a></li>
models.py:
class Post(models.Model):
"""docstring for Post"""
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
post_title = models.CharField(max_length=50)
image = models.ImageField(upload_to = 'user/',null=True, blank=True, height_field="height_field", width_field="width_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
content = models.TextField()
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=True, auto_now_add=False)
def __str__(self):
return (self.post_title)
def get_absolute_url(self):
return reverse("posts:detailpost", kwargs={"id": self.id})
{% url %}
标签采用 URL 模式的名称,而不是路径。您应该为模式命名:
url(r'^profile/(?P<user>\d+)/$', profile, name='profile')
并像这样使用标签:
{% url 'profile' user=request.user.id %}