我怎样才能 link 作者 post 两个他的个人资料页面与 django?
How can I link the author of a post two his profile page with django?
我正在尝试 link post 的作者的两个他的个人资料页面,但是当我点击 link 时,得到的是两个 /profiles/ 而不是作者的id。
我的 views.py 看起来像这样:
def userpage(request, id):
profil = get_object_or_404(UserProfile, pk=id)
context = {'profil': profil}
return render(request, 'gaestebuch/userpage.html', context)
我的 urls.py 看起来像这样:
url(r'^profiles/(?P<id>[0-9]+)/$', views.userpage, name='userpage')
我想要 link 的 html 部分如下所示:
{% for e in latest_eintrage_list %}
<li>
<div id="comment_main">
---> <a href="{% url 'gaestebuch:userpage' profil.id %}">{{ e.author }}</a>
<br>
</div>
<a href="{% url 'gaestebuch:result' e.id %}">{{ e.title }}</a>
<br>
<div id="comment_main">
Comments: {{ e.comments.count }} | {{ e.created_date }} | {{ e.get_typ_display }}
</div>
{% if not forloop.last %}
<hr>
{% endif %}
</li>
{% endfor %}
标有箭头的部分是我希望作者link所在的部分。
models.py:
class UserProfile(models.Model):
user = models.OneToOneField(User)
info = models.CharField(max_length=200, blank = False, default=('keine Angabe'))
class Eintrag(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
NEED = 'ND'
GIVE = 'GV'
TYP_CHOICES = (
(NEED, 'Need'),
(GIVE, 'Give'),
)
typ = models.CharField(max_length=2, choices= TYP_CHOICES, default=NEED)
created_date = models.DateTimeField(default=timezone.now)
我收到以下错误消息:
Reverse for 'userpage' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'gaestebuch/profiles/(?P<id>[0-9]+)/$']
很高兴能得到任何帮助:)
在 {% for e in latest_eintrage_list %}
循环中,您确实有一个变量 profil
。因此 profil.id
被视为空字符串,并且 url 标记失败。
可以按照外键从Eintrag.author
外键到User
模型,然后按照一对一字段倒退到UserProfile
模型:
<a href="{% url 'gaestebuch:userpage' e.author.userprofile.id %}">{{ e.author }}</a>
我正在尝试 link post 的作者的两个他的个人资料页面,但是当我点击 link 时,得到的是两个 /profiles/ 而不是作者的id。
我的 views.py 看起来像这样:
def userpage(request, id):
profil = get_object_or_404(UserProfile, pk=id)
context = {'profil': profil}
return render(request, 'gaestebuch/userpage.html', context)
我的 urls.py 看起来像这样:
url(r'^profiles/(?P<id>[0-9]+)/$', views.userpage, name='userpage')
我想要 link 的 html 部分如下所示:
{% for e in latest_eintrage_list %}
<li>
<div id="comment_main">
---> <a href="{% url 'gaestebuch:userpage' profil.id %}">{{ e.author }}</a>
<br>
</div>
<a href="{% url 'gaestebuch:result' e.id %}">{{ e.title }}</a>
<br>
<div id="comment_main">
Comments: {{ e.comments.count }} | {{ e.created_date }} | {{ e.get_typ_display }}
</div>
{% if not forloop.last %}
<hr>
{% endif %}
</li>
{% endfor %}
标有箭头的部分是我希望作者link所在的部分。
models.py:
class UserProfile(models.Model):
user = models.OneToOneField(User)
info = models.CharField(max_length=200, blank = False, default=('keine Angabe'))
class Eintrag(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
NEED = 'ND'
GIVE = 'GV'
TYP_CHOICES = (
(NEED, 'Need'),
(GIVE, 'Give'),
)
typ = models.CharField(max_length=2, choices= TYP_CHOICES, default=NEED)
created_date = models.DateTimeField(default=timezone.now)
我收到以下错误消息:
Reverse for 'userpage' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'gaestebuch/profiles/(?P<id>[0-9]+)/$']
很高兴能得到任何帮助:)
在 {% for e in latest_eintrage_list %}
循环中,您确实有一个变量 profil
。因此 profil.id
被视为空字符串,并且 url 标记失败。
可以按照外键从Eintrag.author
外键到User
模型,然后按照一对一字段倒退到UserProfile
模型:
<a href="{% url 'gaestebuch:userpage' e.author.userprofile.id %}">{{ e.author }}</a>