I am trying to create a to do list watching a tutorial in order to learn the basics of Django.
The issue is that when I log out instead of getting the log in page I get an error 405. Also when I use the back arrow it takes me to the app content.
Any Ideas on how to solve this ? Following is the code
task_lists.html
{% if request.user.is_authenticated %}
<p>{{request.user}}</p>
<a href="{% url 'logout' %}">Logout</a>
{% else %}
<a href="{% url 'login' %}">Login</a>
{% endif %}
<hr>
<h1>My To DO List</h1>
<a href="{% url 'task-create' %}">Add Task</a>
<table>
<tr>
<th>Item</th>
<th></th>
<th></th>
</tr>
{% for task in tasks %}
<tr>
<td>{{task.title}}</td>
<td><a href="{% url 'tasks' task.id %}">View</a></td>
<td><a href="{% url 'task-update' task.id %}">Edit</a></td>
<td><a href="{% url 'task-delete' task.id %}">Delete</a></td>
</tr>
{% empty %}
<h1>No items in list </h1>
{% endfor %}
</table>
login.html
<h1>Login</h1>
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Login">
</form>
urls.py
from django.urls import path
from .views import TaskList, TaskDetails, TaskCrete, TaskUpdate, DeleteView, CustomLoginView
from django.contrib.auth.views import LogoutView
urlpatterns = [
path('login/', CustomLoginView.as_view(), name='login'),
path('logout/', LogoutView.as_view(next_page='login'), name='logout'),
path('', TaskList.as_view(), name='tasks'),
path('task/<int:pk>/', TaskDetails.as_view(), name='tasks'),
path('task-create/', TaskCrete.as_view(), name='task-create'),
path('task-update/<int:pk>/', TaskUpdate.as_view(), name='task-update'),
path('task-delete/<int:pk>/', DeleteView.as_view(), name='task-delete'),
]
views.py
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.contrib.auth.views import LoginView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Task
class CustomLoginView(LoginView):
template_name = 'base/login.html'
fields = '__all__'
redirect_authenticated_user = True
def get_success_url(self):
return reverse_lazy('tasks')
class TaskList(LoginRequiredMixin, ListView):
model = Task
context_object_name = 'tasks'
class TaskDetails(DetailView):
model = Task
context_object_name = 'task'
template_name = 'base/task.html'
class TaskCrete(CreateView):
model = Task
fields = '__all__'
success_url = reverse_lazy('tasks')
class TaskUpdate(UpdateView):
model = Task
fields = '__all__'
success_url = reverse_lazy('tasks')
class DeleteView(DeleteView):
model = Task
context_object_name = 'task'
success_url = reverse_lazy('tasks')