Login needed to assign authority
(terminal)
-'dir' to check we are on the same folder with manage.py.
path('members/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
TEMPLATES = [
{ ~
'DIRS': [BASE_DIR/'templates'],
INSTALLED_APPS = [
'django.contrib.admin',
~
'users',
]
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('django.contrib.auth.urls')),
path('users/', include('users.urls')),
]
create new templates dir
create registration dir inside
create login.html file, registration.html
(base.html will find old one in another app.)
Copy and paste one of the form html
login, registrtion both needs form.
< div class="form-group">
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<br/>
<button class="btn btn-secondary"> Register </button>
</form>
```
generic.CreateView
we redirect to login page, after registration, you will want to login.
from django.views import generic
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
class UserRegisterView(generic.CreateView):
form_class = UserCreationForm
template_name = 'registration/register.html'
success_url = reverse_lazy('login')
path('register/', UserRegisterView.as_view(), name="register"),
for login, we don't need to do anythin. simply href
{% url 'login' %}
This does magic!
Because we made this auth here.urlpatterns = [ ~ path('members/', include('django.contrib.auth.urls'))
<li class="nav-item">
<a class="nav-link active" aria-current="page"
href="{% url 'register' %}">Register</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page"
href="{% url 'login' %}">Login</a>
</li>
add them at the bottom
LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'
added a new user
urlpatterns = [
path('members/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
LOOUT_REDIRECT_URL = 'home'
<li class="nav-item">
<a class="nav-link active"
aria-current="page" href="{% url 'logout' %}">Logout</a>
</li>
{% if user.is_authenticated %}
{% else %}
{% endif %}
<ul class="navbar-nav">
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link active" aria-current="page"
href="{% url 'add_post' %}">Add Post</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page"
href="{% url 'logout' %}">Logout</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link active" aria-current="page"
href="{% url 'register' %}">Register</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page"
href="{% url 'login' %}">Login</a>
</li>
{% endif %}
</ul>
add_page, edit, delete
{% if user.is_authenticated %}
{% extends 'base.html' %}
{% block title %} Update Post {% endblock %}
{% block content %}
{% if user.is_authenticated %}
<h1> Update Post </h1>
<br/>
<div class="form-group">
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<br/>
<button class="btn btn-secondary"> Update </button>
</form>
</div>
{% else %}
You are not allowed here. Please log in.
{% endif %}
{% endblock %}
{{ post.body|slice:"100" }}