开发者

Django: display/edit form depending on whether user is authenticated?

开发者 https://www.devze.com 2022-12-21 00:54 出处:网络
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.

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 %}
0

精彩评论

暂无评论...
验证码 换一张
取 消