第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使我的評論模型表單在模板中呈現(xiàn)時顯示當(dāng)前用戶的名稱

如何使我的評論模型表單在模板中呈現(xiàn)時顯示當(dāng)前用戶的名稱

梵蒂岡之花 2023-09-02 16:25:28
我正在嘗試在 Django 中創(chuàng)建評論表單。我已經(jīng)呈現(xiàn)了表單,但我希望表單顯示當(dāng)前登錄用戶的名稱,以便我能夠?qū)⒚總€評論與用戶相關(guān)聯(lián)。這是我的模型: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)這是呈現(xiàn)的形式:class ReviewForm(ModelForm):    class Meta:        model = Review        fields = '__all__'
查看完整描述

1 回答

?
慕森卡

TA貢獻(xiàn)1806條經(jīng)驗 獲得超8個贊

在模板中,


{% if user.is_authenticated %}

     <p>{{ user.get_username }} </p>

{% endif %}

在觀點中,


request.user.get_username()


查看完整回答
反對 回復(fù) 2023-09-02
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號