Django: display/edit form depending on whether user is authenticated?
I have a form in Django. If the user is authenticated, I want them to see a form that they can edit: if they aren't, then I would like them to see a display-only form.
In both cases, I want to show the same information, just that if they are authenticated then I want the form to be editable.
Is there an easy way to do this in Django? I can check user.is_authenticated either in the template or in the view: what I don't want to do is duplicate form code in the template.
I found this question, but the accepted answer looks insane开发者_高级运维ly complicated. Surely there must be something inbuilt in Django to handle this problem?
Maybe something like this:
{% if user.is_authenticated %}
{{form.as_table}}
{% else %}
{% for field_name, value in form.data.iteritems %}
{{field_name}}: {{value}}
{% endfor %}
{% endif %}
精彩评论