Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow only Admin (or some user) to edit the pages in Django?

In Django webpage(page can view by all), need to allow only certain people to edit the webpage and other people can just view the webpage.

In Django-admin page's Users table,I've changed the certain users to "Can view" option and added certain users "Can edit" option. It is not working as per expected.

For reference: enter image description here

Getting this error,


POST "/admin/auth/user/4/change/ HTTP/1.1" 302

Actual result to be is

 POST "/admin/auth/user/4/change/ HTTP/1.1" 200

And that change result needs to be changed successfully and reflect

like image 656
gomathi subramanian Avatar asked Dec 06 '25 20:12

gomathi subramanian


1 Answers

views.py

from django.views.generic.edit import UpdateView
from myapp.models import Author

class AuthorUpdate(UpdateView):
    model = Author
    fields = ['name']
    template_name_suffix = '_update_form'

app/author_update_form.html

{% if request.user.is_authenticated and request.user.is_admin %}
<form method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Update">
</form>
{% else %}
<p>Display data</p>
{% endif %}
like image 100
joshlsullivan Avatar answered Dec 09 '25 13:12

joshlsullivan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!