login: make login required and add a login html
This commit is contained in:
parent
cc93abc752
commit
1527f07fa3
|
@ -119,3 +119,5 @@ USE_TZ = True
|
|||
# https://docs.djangoproject.com/en/3.1/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
|
||||
LOGIN_URL = '/accounts/login/'
|
||||
|
|
|
@ -18,5 +18,6 @@ from django.urls import path, include
|
|||
|
||||
urlpatterns = [
|
||||
path('writing/', include('writingtogether.urls')),
|
||||
path('accounts/', include('django.contrib.auth.urls')),
|
||||
path('admin/', admin.site.urls),
|
||||
]
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
{% block content %}
|
||||
|
||||
{% if form.errors %}
|
||||
<p>Your username and password didn't match. Please try again.</p>
|
||||
{% endif %}
|
||||
|
||||
{% if next %}
|
||||
{% if user.is_authenticated %}
|
||||
<p>Your account doesn't have access to this page. To proceed,
|
||||
please login with an account that has access.</p>
|
||||
{% else %}
|
||||
<p>Please login to see this page.</p>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
<form method="post" action="{% url 'login' %}">
|
||||
{% csrf_token %}
|
||||
<table>
|
||||
<tr>
|
||||
<td>{{ form.username.label_tag }}</td>
|
||||
<td>{{ form.username }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{{ form.password.label_tag }}</td>
|
||||
<td>{{ form.password }}</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<input type="submit" value="login">
|
||||
<input type="hidden" name="next" value="{{ next }}">
|
||||
</form>
|
||||
|
||||
{# Assumes you setup the password_reset view in your URLconf #}
|
||||
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
|
||||
|
||||
{% endblock %}
|
|
@ -9,8 +9,9 @@ User = get_user_model()
|
|||
|
||||
class TestViews(TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.user1 = User.objects.create(username='player1')
|
||||
self.user2 = User.objects.create(username='player2')
|
||||
self.user1 = User.objects.create_user(username='player1', password='12345')
|
||||
self.user2 = User.objects.create_user(username='player2')
|
||||
self.client.login(username='player1', password='12345')
|
||||
|
||||
def test_create_story_round_two_rounds(self):
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django import forms
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
|
||||
|
@ -10,7 +11,7 @@ from django.views.generic import CreateView, UpdateView, RedirectView
|
|||
from writingtogether.models import Story, StoryPart, StoryRound
|
||||
|
||||
|
||||
class IndexView(generic.ListView):
|
||||
class IndexView(LoginRequiredMixin, generic.ListView):
|
||||
template_name = 'writingtogether/index.html'
|
||||
context_object_name = 'open_story_round_list'
|
||||
|
||||
|
@ -19,12 +20,12 @@ class IndexView(generic.ListView):
|
|||
return StoryRound.objects.order_by('-created')[:5]
|
||||
|
||||
|
||||
class DetailView(generic.DetailView):
|
||||
class DetailView(LoginRequiredMixin, generic.DetailView):
|
||||
model = StoryRound
|
||||
template_name = 'writingtogether/detail.html'
|
||||
|
||||
|
||||
class StoryRoundCreate(CreateView):
|
||||
class StoryRoundCreate(LoginRequiredMixin, CreateView):
|
||||
model = StoryRound
|
||||
fields = ['name', 'participants', 'number_of_rounds']
|
||||
success_url = reverse_lazy('writing:index')
|
||||
|
@ -53,12 +54,12 @@ class StoryRoundCreate(CreateView):
|
|||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
|
||||
class StoryUpdate(UpdateView):
|
||||
class StoryUpdate(LoginRequiredMixin, UpdateView):
|
||||
model = Story
|
||||
fields = ['name']
|
||||
|
||||
|
||||
class StoryPartUpdate(UpdateView):
|
||||
class StoryPartUpdate(LoginRequiredMixin, UpdateView):
|
||||
model = StoryPart
|
||||
fields = ['text']
|
||||
template_name = 'writingtogether/story_part.html'
|
||||
|
@ -70,7 +71,7 @@ class StoryPartUpdate(UpdateView):
|
|||
# return super().form_valid(form)
|
||||
|
||||
|
||||
class RedirectToNextOpenPart(RedirectView):
|
||||
class RedirectToNextOpenPart(LoginRequiredMixin, RedirectView):
|
||||
permanent = False
|
||||
query_string = True
|
||||
pattern_name = 'writing:update_story_part'
|
||||
|
|
Loading…
Reference in New Issue