将用户重定向到其他模型中的某人个人资料时出错

error when Redirecting user to someone profile in other model

我想将用户重定向到问题模型中的个人资料页面,我希望当用户在问题模型的用户名中单击 url 时,我希望 url 将用户重定向到那个问题的作者 his/her public_profile 像 facebook 这样的页面你可以通过点击他的名字来访问某人的个人资料当 he/she post 什么的时候,我试过使用这个方法但是它抛出一个错误: Reverse for 'Public_Profile' with arguments '('',)' not found.尝试了 1 种模式:['userProfile/(?P[-a-zA-Z0-9_]+)/\Z']

这是我尝试过的:

我的 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'),
]

我的index/home模板:

    <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)

在模板中

<p class="card-title">
 <a href="/profile/{{question.user}}">
   {{question.user.username.upper}}
 </a>
</p>

观看次数:

def profile(request, pk):
    profiles = Profile.objects.get(user=pk)
    questions = Question.objects.filter(user=pk)
    context = {
        'profiles':profiles,
        'questions':questions
    }
    return render(request, 'profile.html', context)

错误试图告诉你 django 试图调用 Public_Profile 但它需要一些 slug。这是因为 profile 在模板上下文中是 None。您需要将 <a href="{% url 'Public_Profile' profile.slug %}"> 更改为 <a href="{% url 'Public_Profile' question.user.slug %}">

您应该将 profile.slug 更改为 question.user.profile.slug

在您的模板中试试这个:

<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' question.user.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>