Python 如何使我的审阅模型表单在模板中呈现时显示当前用户的名称

Python 如何使我的审阅模型表单在模板中呈现时显示当前用户的名称,python,django,Python,Django,我正在尝试用Django创建一个审阅表单。我已经呈现了表单,但我希望表单显示当前登录用户的名称,以使我能够将每个审阅与一个用户关联 这是我的型号: class Review(models.Model): company = models.ForeignKey(Company, null=True, on_delete=models.SET_NULL) # SET_NULL ensures that when a company is deleted, their reviews

我正在尝试用Django创建一个审阅表单。我已经呈现了表单,但我希望表单显示当前登录用户的名称,以使我能够将每个审阅与一个用户关联

这是我的型号:

class Review(models.Model):
    company = models.ForeignKey(Company, null=True, on_delete=models.SET_NULL) 
    # SET_NULL ensures that when a company is deleted, their reviews remains
    reviewers_name = models.CharField(max_length=250, verbose_name='Reviewed By: (Your Name)')
    review_text = models.TextField(max_length=500, verbose_name='Your Review: (Maximum of 200 Words)')
    rating = Int_max.IntegerRangeField(min_value=1, max_value=5)
    date_added = models.DateField('Review Date', auto_now_add=True)
def submit_review(request):
    form = ReviewForm()
    if request.method == 'POST':
        form = ReviewForm(request.POST)
        if form.is_valid:
            form.save()
            # gets the company that was immediately submitted in the review form
            company = request.POST.get('company')
            
            # gets the rating that was immediately submitted in the review form
            rating = request.POST.get('rating')
            
            # uses the name of the company submitted to instantiate the company from the Company database
            companyone = Company.objects.get(pk=company)
            
            """
            emloys companyone above to retrieve already existing average rating associated with it
            adds this to the current rating sent by the user and stores the total back to the average 
            rating field of companyone
            """
            companyone.average_rating = round((int(rating) + int(companyone.average_rating))/2)
            companyone.save()
            
            return redirect('review-submitted')
    context = {
        'form': form
    }
    return render(request, 'submit-review.html', context)
class ReviewForm(ModelForm):
    class Meta:
        model = Review
        fields = '__all__'
以下是我的观点:

class Review(models.Model):
    company = models.ForeignKey(Company, null=True, on_delete=models.SET_NULL) 
    # SET_NULL ensures that when a company is deleted, their reviews remains
    reviewers_name = models.CharField(max_length=250, verbose_name='Reviewed By: (Your Name)')
    review_text = models.TextField(max_length=500, verbose_name='Your Review: (Maximum of 200 Words)')
    rating = Int_max.IntegerRangeField(min_value=1, max_value=5)
    date_added = models.DateField('Review Date', auto_now_add=True)
def submit_review(request):
    form = ReviewForm()
    if request.method == 'POST':
        form = ReviewForm(request.POST)
        if form.is_valid:
            form.save()
            # gets the company that was immediately submitted in the review form
            company = request.POST.get('company')
            
            # gets the rating that was immediately submitted in the review form
            rating = request.POST.get('rating')
            
            # uses the name of the company submitted to instantiate the company from the Company database
            companyone = Company.objects.get(pk=company)
            
            """
            emloys companyone above to retrieve already existing average rating associated with it
            adds this to the current rating sent by the user and stores the total back to the average 
            rating field of companyone
            """
            companyone.average_rating = round((int(rating) + int(companyone.average_rating))/2)
            companyone.save()
            
            return redirect('review-submitted')
    context = {
        'form': form
    }
    return render(request, 'submit-review.html', context)
class ReviewForm(ModelForm):
    class Meta:
        model = Review
        fields = '__all__'
下面是呈现的表单:

class Review(models.Model):
    company = models.ForeignKey(Company, null=True, on_delete=models.SET_NULL) 
    # SET_NULL ensures that when a company is deleted, their reviews remains
    reviewers_name = models.CharField(max_length=250, verbose_name='Reviewed By: (Your Name)')
    review_text = models.TextField(max_length=500, verbose_name='Your Review: (Maximum of 200 Words)')
    rating = Int_max.IntegerRangeField(min_value=1, max_value=5)
    date_added = models.DateField('Review Date', auto_now_add=True)
def submit_review(request):
    form = ReviewForm()
    if request.method == 'POST':
        form = ReviewForm(request.POST)
        if form.is_valid:
            form.save()
            # gets the company that was immediately submitted in the review form
            company = request.POST.get('company')
            
            # gets the rating that was immediately submitted in the review form
            rating = request.POST.get('rating')
            
            # uses the name of the company submitted to instantiate the company from the Company database
            companyone = Company.objects.get(pk=company)
            
            """
            emloys companyone above to retrieve already existing average rating associated with it
            adds this to the current rating sent by the user and stores the total back to the average 
            rating field of companyone
            """
            companyone.average_rating = round((int(rating) + int(companyone.average_rating))/2)
            companyone.save()
            
            return redirect('review-submitted')
    context = {
        'form': form
    }
    return render(request, 'submit-review.html', context)
class ReviewForm(ModelForm):
    class Meta:
        model = Review
        fields = '__all__'
在模板中

{% if user.is_authenticated %}
     <p>{{ user.get_username }} </p>
{% endif %}

{%if user.is_authenticated%}{{user.get_username}

{%endif%}这完成了任务,谢谢。。。不需要视图部分。再次谢谢你!