#1: add login page; require login for views

This commit is contained in:
Lexi / Zoe 2019-11-10 23:05:39 +01:00
parent c4ae7f2759
commit faea970aff
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
5 changed files with 31 additions and 7 deletions

View File

@ -1,3 +1,5 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views import generic
@ -6,7 +8,7 @@ from .forms import IssueForm
from .models import Issue
class IndexView(generic.ListView):
class IndexView(LoginRequiredMixin, generic.ListView):
template_name = 'issues/index.html'
context_object_name = 'issue_list'
@ -14,17 +16,18 @@ class IndexView(generic.ListView):
return Issue.objects.order_by('create_date')
class DetailView(generic.DetailView):
class DetailView(LoginRequiredMixin, generic.DetailView):
model = Issue
template_name = 'issues/detail.html'
pk_url_kwarg = 'issue_id'
# class NewIssueForm(generic.FormView):
# class NewIssueForm(LoginRequiredMixin, generic.FormView):
# template_name = 'issues/new.html'
# form_class = IssueForm
# success_url =
@login_required
def new(request):
if request.method == 'POST':
form = IssueForm(request.POST)

View File

@ -1,9 +1,9 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views import generic
from .models import Project
class IndexView(generic.ListView):
class IndexView(LoginRequiredMixin, generic.ListView):
template_name = 'projects/index.html'
context_object_name = 'project_list'
@ -11,7 +11,7 @@ class IndexView(generic.ListView):
return Project.objects.order_by('project_key')
class ProjectView(generic.DetailView):
class ProjectView(LoginRequiredMixin, generic.DetailView):
model = Project
template_name = 'projects/view.html'
slug_field = slug_url_kwarg = 'project_key'

View File

@ -0,0 +1,7 @@
<h2>Login</h2>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>

View File

@ -56,7 +56,9 @@ ROOT_URLCONF = 'tofu.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
@ -120,3 +122,9 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
# Other settings
LOGIN_REDIRECT_URL = '/projects/'
LOGOUT_REDIRECT_URL = '/account/login/'

View File

@ -14,10 +14,16 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.shortcuts import redirect
from django.urls import include, path
urlpatterns = [
path('account/', include('django.contrib.auth.urls')),
path('issues/', include('issues.urls')),
path('projects/', include('projects.urls')),
path('admin/', admin.site.urls),
# TODO temporarily: just redirect / to /projects/
path('', lambda request: redirect('projects/', permanent=False)),
]