【问题标题】:Django 1.5. Users attributes in comments framework姜戈 1.5。评论框架中的用户属性
【发布时间】:2013-02-27 12:08:03
【问题描述】:

我在 Django 1.5 中使用 MyUser 模型(电子邮件作为登录名):

class MyUserManager(BaseUserManager):
 def create_user(self, email, password=None):
    """
    Creates and saves a User with the given email, date of
    birth and password.
    """
    if not email:
        raise ValueError('Users must have an email address')

    user = self.model(
        email=MyUserManager.normalize_email(email),
       # date_of_birth=date_of_birth,
    )

    user.set_password(password)
    user.save(using=self._db)
    return user

def create_superuser(self, email, password):
    """
    Creates and saves a superuser with the given email, date of
    birth and password.
    """
    user = self.create_user(email,
        password=password,
        #date_of_birth=date_of_birth
    )
    user.is_admin = True
    user.save(using=self._db)
    return user


 class MyUser(AbstractBaseUser):
   email = models.EmailField(
    verbose_name='email address',
    max_length=255,
    unique=True,
    db_index=True,
)
last_name=models.CharField(max_length=30)
first_name=models.CharField(max_length=30)
second_name=models.CharField(max_length=30, blank=True)
about=models.TextField(blank=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)  

objects = MyUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['last_name','first_name','second_name',]

def get_full_name(self):
    # The user is identified by their email address
    return self.email

def get_short_name(self):
    # The user is identified by their email address
    return self.email

def __unicode__(self):
    return self.email

def has_perm(self, perm, obj=None):
    "Does the user have a specific permission?"
    # Simplest possible answer: Yes, always
    return True

def has_module_perms(self, app_label):
    "Does the user have permissions to view the app `app_label`?"
    # Simplest possible answer: Yes, always
    return True

@property
def is_staff(self):
    "Is the user a member of staff?"
    # Simplest possible answer: All admins are staff
    return self.is_admin  

settings.py:

AUTH_USER_MODEL = 'app.MyUser'

我激活了 django cmets 框架:

settings.py:

'django.contrib.comments',

网址:

(r'^comments/', include('django.contrib.comments.urls')),

模板(只有授权用户才能添加评论):

<h2>Add a comment:</h2> {%  get_comment_form for post as form %} 
<form action="{% comment_form_target %}" method="post" > {% csrf_token %}   
{% if next %}
<div><input type="hidden" name="next" value="{{ next }}" /></div>
{% endif %}     
{{form.content_type}}{{form.object_pk}}{{form.timestamp}}{{form.security_hash}}
Comment:<br />
{{form.comment}}       
<input type="hidden" name="next" value="{{ request.get_full_path }}" /> 

<input type="submit" name="submit" class="submit-post" value="Post"  />
<input type="submit" name="preview" class="submit-preview" value="Preview" />
</form> 

{% get_comment_count for post as comment_count %}   
<h2>Comments: [{{ comment_count }}]</h2> 
{% get_comment_list for post as comment_list %} 
{% for comment in comment_list|dictsortreversed:"submit_date" %}
<dl id="comments">      
{{ comment.email }} {{ comment.submit_date|date:"d.m.Y G:i" }}
<dd>
{{ comment.comment|striptags|urlizetrunc:20|linebreaksbr }}
</dd>
</dl>
{% endfor %}

如何获取用户的模型字段“first_name”等? comment.email 和 comment.name 给了我“电子邮件”字段,comment.first_name 什么也没给我。谢谢!

【问题讨论】:

    标签: django model django-comments


    【解决方案1】:

    根据built-in comment model 文档,您可以通过模板中的{{ comment.user }} 访问用户发布的评论。因此,您可以像{{ comment.user.email }}{{ comment.user.first_name }} 等那样访问MyUser 模型字段。

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 2020-10-02
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多