将用户重定向到其他模型中的某人配置文件时出错
我想将用户重定向到问题模型中的个人资料页面,当用户在问题模型的用户名中单击URL时,我希望该URL将用户重定向到该问题的作者到他/她的public_profile页面,例如Facebook您可以在他/她发布某些内容时单击他的名字来访问某人的个人资料,我尝试使用此方法,但它给我带来了一个错误:反向'public_profile'带有参数'(''',)'找不到。 个模式尝试:['userProfile/
(
?
urlpatterns = [
path('', views.rules, name='Rules'),
path('create', views.login, name='login'),
path('index/', views.index, name='index'),
path('view/<slug:slug>/', views.viewQuestion, name='view-Question'),
path('question/<int:pk>/answer/', views.My_Answer.as_view(), name='answer'),
path('question/', views.My_Question.as_view(), name='question'),
path('register/', views.register, name='register'),
path('feedback/', views.PostFeedBack.as_view(), name='FeedBack'),
path('notification/', views.NotificationListView.as_view(), name='notification'),
path('profile/<int:pk>/', views.profile, name='Profile'),
path('edit/<slug:slug>/', views.EditProfile.as_view(), name='edit'),
path('userProfile/<slug:slug>/', views.public_profile, name='Public_Profile'),
]
1 :
<div class="container">
<div class="row justify-content-center">
{% for question in list_of_question reversed %}
<div class="col-md-4">
<div class="card my-3">
<div class="card-header">
<p class="card-title"><a href="{% url 'Public_Profile' profile.slug %}">
{{question.user.username.upper}}
</a></p>
</div>
<div class="card-body">
<a href="{% url 'view-Question' question.slug %}" style="text-decoration: none;">
<p class="card-title">{{question.title}}</p>
</a>
<h5>{{question.category}}</h5>
</div>
</div>
</div>
{%endfor%}
</div>
</div>
我的观点:
@login_required(login_url='login')
def index(request):
query = request.GET.get('q', None)
list_of_question = Question.objects.all()
if query is not None:
list_of_question = Question.objects.filter(
Q(title__icontains=query) |
Q(category__name__icontains=query)
)
unread_notifications =
Notification.objects.filter(user=request.user,
is_read=False).count()
paginator = Paginator(list_of_question, per_page=15)
context = [
{
'unread_notifications':unread_notifications,
'list_of_question':list_of_question,
'paginator':paginator
}
]
return render(request, 'index.html', context)
def profile(request, pk):
profiles = Profile.objects.filter(user=request.user)
questions = Question.objects.filter(user=request.user)
context = {
'profiles':profiles,
'questions':questions
}
return render(request, 'profile.html', context)
def public_profile(request, slug):
contents = get_object_or_404(Profile, slug=slug)
profiles = Profile.objects.filter(user=contents)
context = {
'contents':contents,
'profiles':profiles
}
return render(request, 'public_profile.html', context)
我的模型
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to='avatar', blank=True, null=True,
default='static/default.jpg')
stories = RichTextField(blank=True, null=True)
twitter = models.URLField(max_length=300, blank=True, null=True)
website = models.URLField(max_length=300,blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
location = models.CharField(max_length=80, blank=True, null=True)
slug = models.SlugField(unique=True, max_length=200)
def save(self, *args, **kwargs):
self.slug = slugify(self.user)
super(Profile, self).save(*args, **kwargs)
def __str__(self):
return str(self.user)
class Question(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100, blank=False, null=False)
body = RichTextField(blank=False, null=False)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
slug = models.SlugField(unique=True, max_length=200)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Question, self).save(*args, **kwargs)
def __str__(self):
return str(self.title)
I want to redirected user to the profile page in the question model, I want when user click on url in the username of the question model, I want that url to redirected user to the author of that question to his/her public_profile page like facebook you can visit someone profile by clicking on his name when he/she post something, I have try using this method but it throws me an error: Reverse for 'Public_Profile' with arguments '('',)' not found. 1 pattern(s) tried: ['userProfile/(?P[-a-zA-Z0-9_]+)/\Z']
this is what i have try:
my urls:
urlpatterns = [
path('', views.rules, name='Rules'),
path('create', views.login, name='login'),
path('index/', views.index, name='index'),
path('view/<slug:slug>/', views.viewQuestion, name='view-Question'),
path('question/<int:pk>/answer/', views.My_Answer.as_view(), name='answer'),
path('question/', views.My_Question.as_view(), name='question'),
path('register/', views.register, name='register'),
path('feedback/', views.PostFeedBack.as_view(), name='FeedBack'),
path('notification/', views.NotificationListView.as_view(), name='notification'),
path('profile/<int:pk>/', views.profile, name='Profile'),
path('edit/<slug:slug>/', views.EditProfile.as_view(), name='edit'),
path('userProfile/<slug:slug>/', views.public_profile, name='Public_Profile'),
]
my index/home template:
<div class="container">
<div class="row justify-content-center">
{% for question in list_of_question reversed %}
<div class="col-md-4">
<div class="card my-3">
<div class="card-header">
<p class="card-title"><a href="{% url 'Public_Profile' profile.slug %}">
{{question.user.username.upper}}
</a></p>
</div>
<div class="card-body">
<a href="{% url 'view-Question' question.slug %}" style="text-decoration: none;">
<p class="card-title">{{question.title}}</p>
</a>
<h5>{{question.category}}</h5>
</div>
</div>
</div>
{%endfor%}
</div>
</div>
my views:
@login_required(login_url='login')
def index(request):
query = request.GET.get('q', None)
list_of_question = Question.objects.all()
if query is not None:
list_of_question = Question.objects.filter(
Q(title__icontains=query) |
Q(category__name__icontains=query)
)
unread_notifications =
Notification.objects.filter(user=request.user,
is_read=False).count()
paginator = Paginator(list_of_question, per_page=15)
context = [
{
'unread_notifications':unread_notifications,
'list_of_question':list_of_question,
'paginator':paginator
}
]
return render(request, 'index.html', context)
def profile(request, pk):
profiles = Profile.objects.filter(user=request.user)
questions = Question.objects.filter(user=request.user)
context = {
'profiles':profiles,
'questions':questions
}
return render(request, 'profile.html', context)
def public_profile(request, slug):
contents = get_object_or_404(Profile, slug=slug)
profiles = Profile.objects.filter(user=contents)
context = {
'contents':contents,
'profiles':profiles
}
return render(request, 'public_profile.html', context)
my model
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to='avatar', blank=True, null=True,
default='static/default.jpg')
stories = RichTextField(blank=True, null=True)
twitter = models.URLField(max_length=300, blank=True, null=True)
website = models.URLField(max_length=300,blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
location = models.CharField(max_length=80, blank=True, null=True)
slug = models.SlugField(unique=True, max_length=200)
def save(self, *args, **kwargs):
self.slug = slugify(self.user)
super(Profile, self).save(*args, **kwargs)
def __str__(self):
return str(self.user)
class Question(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100, blank=False, null=False)
body = RichTextField(blank=False, null=False)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
slug = models.SlugField(unique=True, max_length=200)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Question, self).save(*args, **kwargs)
def __str__(self):
return str(self.title)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
错误是试图告诉您Django试图致电 public_profile ,但需要一些sl。这是因为
profile
在模板上下文中没有。您需要更改&lt; a href =“ {%url'public_profile'profile'profile'profile.slug%}”&gt;
使用&lt; a href =“ {%url url'public_profile'问题.slug%}“&gt;
The error is trying to tell you that django tried to call Public_Profile but it requires some slug. This is because
profile
is None in the template context. You need to change<a href="{% url 'Public_Profile' profile.slug %}">
with<a href="{% url 'Public_Profile' question.user.slug %}">
您应该更改profile.slug toed.user.profile.slug
在模板中尝试此操作:
You should change profile.slug into question.user.profile.slug
Try this in your template:
在模板
视图中:
In template
Views: