Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a55fa7394 | ||
|
|
5d99369516 |
@@ -37,7 +37,8 @@ INSTALLED_APPS = [
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'posts.apps.PostsConfig' #you could also add 'posts' to register everything in your app
|
||||
'posts.apps.PostsConfig', #you could also add 'posts' to register everything in your app
|
||||
'rest_framework'
|
||||
|
||||
]
|
||||
|
||||
|
||||
+3
-2
@@ -19,6 +19,7 @@ from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('', views.index, name='posts'),
|
||||
path('posts/', include('posts.urls'), name='posts')
|
||||
path('', views.index),
|
||||
path('posts/', include('posts.urls')),
|
||||
path('accounts/', include('django.contrib.auth.urls'))
|
||||
]
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
from rest_framework import serializers
|
||||
from .models import Notice
|
||||
|
||||
class NoticeSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Notice
|
||||
fields = ('id', 'notice_title', 'notice_text', 'pub_start', 'pub_end')
|
||||
+3
-3
@@ -4,8 +4,8 @@ from django.urls import path, re_path
|
||||
urlpatterns = [
|
||||
# use re_path for regex (.*) - this regex accepts anything - see https://docs.python.org/3/library/re.html for more
|
||||
path('', views.home, name='index'),
|
||||
re_path('delete/(?P<value>\d+)/$', views.delete, name='delete'),
|
||||
re_path('(?P<value>.*)/$', views.home, name='textausgabe'),
|
||||
path('delete/<int:value>', views.delete, name='delete'),
|
||||
path('new', views.new, name='new'),
|
||||
# only accept numbers ^[0-9]*$
|
||||
path('notices/', views.notice_list, name='index'),
|
||||
path('notices/<int:id>', views.notice_detail, name='delete'),
|
||||
]
|
||||
+50
-2
@@ -1,9 +1,15 @@
|
||||
from django.http import JsonResponse, HttpResponse
|
||||
from django.shortcuts import render, redirect
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
from rest_framework.parsers import JSONParser
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from posts.models import Notice
|
||||
from posts.forms import NoticeForm
|
||||
from django.http import HttpResponse
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from posts.serializers import NoticeSerializer
|
||||
# Create your views here.
|
||||
|
||||
|
||||
@@ -21,6 +27,7 @@ def home(request, value=""):
|
||||
}
|
||||
return render(request, 'index.html', context)
|
||||
|
||||
@login_required
|
||||
def new(request):
|
||||
if request.method == "POST":
|
||||
form = NoticeForm(request.POST)
|
||||
@@ -34,7 +41,48 @@ def new(request):
|
||||
context = {'form': NoticeForm()}
|
||||
return render(request, 'edit.html', context)
|
||||
|
||||
@staff_member_required
|
||||
def delete(request, value):
|
||||
notice = Notice.objects.get(id = value)
|
||||
notice.delete()
|
||||
return redirect('index')
|
||||
return redirect('index')
|
||||
|
||||
@csrf_exempt
|
||||
def notice_list(request):
|
||||
if request.method == "GET":
|
||||
notices = Notice.objects.all()
|
||||
serializer = NoticeSerializer(notices, many=True)
|
||||
return JsonResponse(serializer.data, safe=False)
|
||||
elif request.method == "POST":
|
||||
data = JSONParser().parse(request)
|
||||
serializer = NoticeSerializer(data=data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return JsonResponse(serializer.data, status=201)
|
||||
return JsonResponse(serializer.errors, status=400)
|
||||
return HttpResponse(status=405)
|
||||
|
||||
@csrf_exempt
|
||||
def notice_detail(request, id):
|
||||
try:
|
||||
notice = Notice.objects.get(id=id)
|
||||
except:
|
||||
return HttpResponse(status=404)
|
||||
|
||||
if request.method == 'GET':
|
||||
serializer = NoticeSerializer(notice)
|
||||
return JsonResponse(serializer.data)
|
||||
|
||||
elif request.method == 'PUT':
|
||||
data = JSONParser().parse(request)
|
||||
serializer = NoticeSerializer(notice, data=data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return JsonResponse(serializer.data)
|
||||
return JsonResponse(serializer.errors, status=400)
|
||||
|
||||
elif request.method == 'DELETE':
|
||||
notice.delete()
|
||||
return HttpResponse(status=204)
|
||||
|
||||
return HttpResponse(status=405)
|
||||
+13
-1
@@ -17,7 +17,7 @@
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav">
|
||||
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
|
||||
<li class="nav-item active">
|
||||
<a class="nav-link" href="/posts">Posts</a>
|
||||
</li>
|
||||
@@ -28,6 +28,18 @@
|
||||
<a class="nav-link" href="https://www.google.com/">Google</a>
|
||||
</li>
|
||||
</ul>
|
||||
<ul class="navbar-nav">
|
||||
{% if request.user.is_authenticated %}
|
||||
<span class="navbar-text"> Willkommen {{ request.user.username }}! </span>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{% url 'logout' %}?next=/posts">Abmelden</a>
|
||||
</li>
|
||||
{% else %}
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{% url 'login' %}?next=/posts">Anmelden</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
{% block content %} {% endblock %}
|
||||
|
||||
@@ -7,8 +7,12 @@
|
||||
<div class="jumbotron">
|
||||
<h1 class="display-6">{{ notice.notice_title }}</h1>
|
||||
<p class="lead">{{notice.notice_text}}</p>
|
||||
<p><a href="{% url 'delete' notice.id %}" class="btn btn-info" role="button">Nachricht löschen</a></p>
|
||||
{% if user.is_staff %}
|
||||
<p><a href="{% url 'delete' notice.id %}" class="btn btn-info" role="button">Nachricht löschen</a></p>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p>
|
||||
{% if user.is_authenticated %}
|
||||
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p>
|
||||
{% endif %}
|
||||
{% endblock content %}
|
||||
@@ -0,0 +1,12 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block title %} Login {% endblock %}
|
||||
{% block content %}
|
||||
<div class="container"><form method="post" action="{% url 'login' %}">
|
||||
{% csrf_token %}
|
||||
Benutzername: {{ form.username }}
|
||||
<br>Passwort: {{ form.password }} <br>
|
||||
<button type="submit" class="btn btn-default">Anmelden</button>
|
||||
<input type="hidden" name="next" value="{{ next }}" />
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user