#1: add login page; require login for views
This commit is contained in:
parent
c4ae7f2759
commit
faea970aff
|
|
@ -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.http import HttpResponseRedirect
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
|
|
@ -6,7 +8,7 @@ from .forms import IssueForm
|
||||||
from .models import Issue
|
from .models import Issue
|
||||||
|
|
||||||
|
|
||||||
class IndexView(generic.ListView):
|
class IndexView(LoginRequiredMixin, generic.ListView):
|
||||||
template_name = 'issues/index.html'
|
template_name = 'issues/index.html'
|
||||||
context_object_name = 'issue_list'
|
context_object_name = 'issue_list'
|
||||||
|
|
||||||
|
|
@ -14,17 +16,18 @@ class IndexView(generic.ListView):
|
||||||
return Issue.objects.order_by('create_date')
|
return Issue.objects.order_by('create_date')
|
||||||
|
|
||||||
|
|
||||||
class DetailView(generic.DetailView):
|
class DetailView(LoginRequiredMixin, generic.DetailView):
|
||||||
model = Issue
|
model = Issue
|
||||||
template_name = 'issues/detail.html'
|
template_name = 'issues/detail.html'
|
||||||
pk_url_kwarg = 'issue_id'
|
pk_url_kwarg = 'issue_id'
|
||||||
|
|
||||||
|
|
||||||
# class NewIssueForm(generic.FormView):
|
# class NewIssueForm(LoginRequiredMixin, generic.FormView):
|
||||||
# template_name = 'issues/new.html'
|
# template_name = 'issues/new.html'
|
||||||
# form_class = IssueForm
|
# form_class = IssueForm
|
||||||
# success_url =
|
# success_url =
|
||||||
|
|
||||||
|
@login_required
|
||||||
def new(request):
|
def new(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = IssueForm(request.POST)
|
form = IssueForm(request.POST)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
|
|
||||||
from .models import Project
|
from .models import Project
|
||||||
|
|
||||||
|
class IndexView(LoginRequiredMixin, generic.ListView):
|
||||||
class IndexView(generic.ListView):
|
|
||||||
template_name = 'projects/index.html'
|
template_name = 'projects/index.html'
|
||||||
context_object_name = 'project_list'
|
context_object_name = 'project_list'
|
||||||
|
|
||||||
|
|
@ -11,7 +11,7 @@ class IndexView(generic.ListView):
|
||||||
return Project.objects.order_by('project_key')
|
return Project.objects.order_by('project_key')
|
||||||
|
|
||||||
|
|
||||||
class ProjectView(generic.DetailView):
|
class ProjectView(LoginRequiredMixin, generic.DetailView):
|
||||||
model = Project
|
model = Project
|
||||||
template_name = 'projects/view.html'
|
template_name = 'projects/view.html'
|
||||||
slug_field = slug_url_kwarg = 'project_key'
|
slug_field = slug_url_kwarg = 'project_key'
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<h2>Login</h2>
|
||||||
|
|
||||||
|
<form method="POST">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form.as_p }}
|
||||||
|
<button type="submit">Login</button>
|
||||||
|
</form>
|
||||||
|
|
@ -56,7 +56,9 @@ ROOT_URLCONF = 'tofu.urls'
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'DIRS': [],
|
'DIRS': [
|
||||||
|
os.path.join(BASE_DIR, 'templates'),
|
||||||
|
],
|
||||||
'APP_DIRS': True,
|
'APP_DIRS': True,
|
||||||
'OPTIONS': {
|
'OPTIONS': {
|
||||||
'context_processors': [
|
'context_processors': [
|
||||||
|
|
@ -120,3 +122,9 @@ USE_TZ = True
|
||||||
# https://docs.djangoproject.com/en/2.1/howto/static-files/
|
# https://docs.djangoproject.com/en/2.1/howto/static-files/
|
||||||
|
|
||||||
STATIC_URL = '/static/'
|
STATIC_URL = '/static/'
|
||||||
|
|
||||||
|
|
||||||
|
# Other settings
|
||||||
|
|
||||||
|
LOGIN_REDIRECT_URL = '/projects/'
|
||||||
|
LOGOUT_REDIRECT_URL = '/account/login/'
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,16 @@ Including another URLconf
|
||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from django.shortcuts import redirect
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
path('account/', include('django.contrib.auth.urls')),
|
||||||
path('issues/', include('issues.urls')),
|
path('issues/', include('issues.urls')),
|
||||||
path('projects/', include('projects.urls')),
|
path('projects/', include('projects.urls')),
|
||||||
|
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
|
|
||||||
|
# TODO temporarily: just redirect / to /projects/
|
||||||
|
path('', lambda request: redirect('projects/', permanent=False)),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Reference in New Issue