对我来说似乎很简单。如果用户已通过身份验证,则会自动填充相关表单字段,并且不会显示它们。如果用户未经过身份验证,请显示表单字段。我一直在寻找各处,我似乎无法找到答案。有谁知道解决这个问题的方法?
答案 0 :(得分:0)
你可以这样做:
{% if user.is_authenticated %}
<form>
<input type="hidden" name="username" value="{{ user.username }}" />
</form>
{% else %}
<!-- use your "regular" form here -->
希望这会有所帮助......
答案 1 :(得分:0)
这个例子来自真实的系统,我一直在努力。希望能帮到你。
import datetime
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import force_unicode
import django.contrib.comments.forms
from current_user.middleware import get_current_user
class AuthCommentForm(django.contrib.comments.forms.CommentForm):
def get_comment_create_data(self):
"""
Returns the dict of data to be used to create a comment. Subclasses in
custom comment apps that override get_comment_model can override this
method to add extra fields onto a custom comment model.
"""
return dict(
content_type=ContentType.objects.get_for_model(self.target_object),
object_pk=force_unicode(self.target_object._get_pk_val()),
user_name=get_current_user().username,
user_email=get_current_user().email,
user_url='',
comment=self.cleaned_data["comment"],
submit_date=datetime.datetime.now(),
site_id=settings.SITE_ID,
is_public=True,
is_removed=False,
)
AuthCommentForm.base_fields.pop('url')
AuthCommentForm.base_fields.pop('name')
AuthCommentForm.base_fields.pop('email')
AuthCommentForm.base_fields.pop('honeypot')
AuthCommentForm.base_fields['comment'].label = False