简体   繁体   English

如何获取写入文本的登录用户的用户名并将其永久显示在 Django 中的文本旁边?

[英]How do i get the username of a logged user writing a text and display it beside the text permanently in Django?

Please see the image, I want to add the name of the person entering the text at the end of the text.请看图片,我想在文本末尾添加输入文本的人的姓名。

  [1]: https://i.stack.imgur.com/qd5fn.png
Here is my code for the template.
{% extends 'learning_logs/base.html' %}

{% block content %}

  <p>Topic: {{ topic }}</p>
  
  <p>Entries:</p>
  <p>
    <a href="{% url 'learning_logs:new_entry' topic.id %}">add new entry</a>
  </p>
  <ul>
  {% for entry in entries %}
    <li>
      <b><p>{{ entry.date_added|date:'M d, Y H:i' }}</p></b>
      <p>{{ entry.text|linebreaks }}</p>
        <a href="{% url 'learning_logs:edit_entry' entry.id %}">edit entry</a>
      </p>
    </li>
  {% empty %}
    <li>
      There are no entries for this topic yet.
    </li>
  {% endfor %}
  </ul>
  

{% endblock content %}

Here is my views function for the topic:以下是我对该主题的看法 function:

def topic(request, topic_id):
    """Show a single topic and all its enteries"""
    topic= Topic.objects.get(id=topic_id)
    entries= topic.entry_set.order_by('-date_added')
    context= {'topic': topic, 'entries':entries}
    user_if= request.user
    return render(request, 'learning_logs/topic.html', context)

Here are my Topic and Entry models:这是我的主题和条目模型:

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Topic(models.Model):
    """A topic user is learning about"""
    text=models.CharField(max_length=200)
    date_added=models.DateTimeField(auto_now_add=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    
    def __str__(self):
        """Return a string representation of the model."""
         return self.text[:50] +"..."
       
class Entry(models.Model):
    """Something specific learned about the topic"""
    topic=models.ForeignKey('Topic',on_delete=models.CASCADE)
    text=models.TextField()
    date_added=models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)



    class Meta:
        verbose_name_plural='entries'

    def __str__(self):
        """Return a string representation of model"""
        return self.text[:50] +"..."


 

I want to add the name of the person entering the text beside the text, I have the username and password of the users stored in the database but I don't know how to display the name of the user permanently beside the text.我想在文本旁边添加输入文本的人的姓名,我有存储在数据库中的用户的用户名和密码,但我不知道如何在文本旁边永久显示用户的姓名。 It is just like comments where the users have their names above the comment.就像评论一样,用户的名字在评论上方。 Do tell me if the question wants more code display, I will add the code.如果问题需要更多代码显示,请告诉我,我会添加代码。

Do you want to dynamically show the user while writing the text.您是否想在编写文本时动态显示用户。 You should then pass request.user in context so that current user who is editing will always be available in the template.然后,您应该在上下文中传递 request.user ,以便正在编辑的当前用户始终在模板中可用。

context = {"topic": topic, "entries": entries, "current_user":request.user}

Now since current_user is available in the template just use it inside the template现在由于 current_user 在模板中可用,只需在模板中使用它

@{{current_user.username}}

If you are saving the username otherwise show whichever field you want to for eg.如果您要保存用户名,则显示您想要的任何字段,例如。 current_user.full_name current_user.full_name

You can always write {{request.user.username}} at any point you want the logged in user's username to appear.您可以随时在您希望登录用户的用户名出现的任何时候写入 {{request.user.username}}。

No need to pass the user object as extra context.无需将用户 object 作为额外上下文传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM