Browse Source

Zwischenstand des Praktikums: index-Fkt muss noch angepasst werden!

master
Jonka Winkle 2 years ago
parent
commit
2e11b651a1

+ 2
- 1
Django_Project/Django_Project/settings.py View File

@@ -33,6 +33,7 @@ ALLOWED_HOSTS = []
INSTALLED_APPS = [

'posts.apps.PostsConfig',
'rest_framework',

'django.contrib.admin',
'django.contrib.auth',
@@ -107,7 +108,7 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'de-de'

TIME_ZONE = 'UTC'


+ 13
- 0
Django_Project/posts/forms.py View File

@@ -0,0 +1,13 @@
from django import forms
import datetime

class NoticeForm(forms.Form):
date_formats = ['%d.%m.%Y', '%d.%m.%y']
title = forms.CharField(label='Titel', max_length=80)
text = forms.CharField(label='Text', max_length=400)
start = forms.DateField(label='Von',
input_formats=date_formats,
initial=datetime.date.today)
end = forms.DateField(label='Bis',
input_formats=date_formats,
initial=datetime.date.today)

+ 7
- 0
Django_Project/posts/serializers.py View File

@@ -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')

+ 6
- 0
Django_Project/posts/urls.py View File

@@ -1,7 +1,13 @@
from django.conf.urls import include, url
from . import views
from django.urls import path, include, re_path

urlpatterns = [
url(r'^$', views.index, name="index"),
path("new", views.new, name="new"),
path("delete/<int:deleteid>", views.delete, name="delete"),

path('notices/', views.notice_list),
path('notices/<int:id>', views.notice_detail),
]


+ 103
- 7
Django_Project/posts/views.py View File

@@ -1,13 +1,109 @@
from posts.models import Notice
from django.utils import timezone
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
from django.http import HttpResponse

def index(request):
#return render(request, 'posts/index.html')

#context = # hier muss Datenbank ausgelesen werden:
# for curr_msg in Notice.objects.all():
# if (curr_msg.pub_start > datetime.now() and curr_msg.pub_end < date......
# context +=
return render(request, 'posts/allMessages.html')
#-------------------- eigener Versuch ---------------------

# relevant_notices = Notice.objects.filter(pub_start__lt = timezone.datetime.now())
# relevant_notices = Notice.objects.filter(pub_end__gt = timezone.datetime.now())

# for curr_msg in relevant_notices:
# context['now'] += "TITEL DER NACHRICHT: " + curr_msg.notice_title + "TEXT DER NACHRICHT: " + curr_msg.notice_text
# # unschöne Lösung, da hier ein Fließtext ohne Layout-Möglichkeiten (z.B. backspace zwischen jeder neuen Zeile) entsteht
# # bessere Variante: for-Schleife in das Template (allMessages.html) auslagern

#------------------ ab hier: schöne Variante --------------------

relevant_notices = Notice.objects.all()
relevant_notices = Notice.objects.filter(pub_start__lt=timezone.datetime.now())
relevant_notices = Notice.objects.filter(pub_end__gt=timezone.datetime.now())
# all diese Befehle importieren noch NHCTS aus der Datenbank! Sie bauen nur einen SQL-Zugriffs-Befehl auf (mit entsprevchender Filterung) und erst beim Auslesen wird auf Datenbank zugegriffen

context = { "notices" : relevant_notices }

return render(request, 'posts/allMessages.html', context)


from posts.forms import NoticeForm
from django.shortcuts import redirect

def new(request):
#return HttpResponse("Eingabe einer neuen Nachricht!")
if request.method == "POST":
eingabeFenster = NoticeForm(request.POST)
if eingabeFenster.is_valid():
newNotice = Notice(notice_title = eingabeFenster.cleaned_data['title'],
notice_text = eingabeFenster.cleaned_data['text'],
pub_start = eingabeFenster.cleaned_data['start'],
pub_end = eingabeFenster.cleaned_data['end'])
newNotice.save()
return redirect('index')
context = {'form' : NoticeForm() }
return render(request, 'posts/edit.html', context)

def delete(request, id):
msg_to_delete = Notice.objects.get(id = id)
msg_to_delete.clean()
return redirect('index')



from posts.models import Notice
from posts.serializers import NoticeSerializer
from django.http import JsonResponse
from rest_framework.parsers import JSONParser

from django.views.decorators.csrf import csrf_exempt

@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)
else:
return JsonResponse(serializer.errors, status=400)

else:
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)
else:
return JsonResponse(serializer.errors, status=400)

elif request.method == 'DELETE':
notice.delete()
return HttpResponse(status=204)

else:
return HttpResponse(status=405)



+ 1
- 1
Django_Project/templates/base.html View File

@@ -28,7 +28,7 @@
</ul>
</nav>

{% block content %} zzz {% endblock %}
{% block content %} zzz {% endblock %}

</body>
</html>

+ 25
- 10
Django_Project/templates/posts/allMessages.html View File

@@ -1,10 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Messages</title>
</head>
<body>

</body>
</html>

{% extends 'base.html' %}

{% block title %}
News
{% endblock %}

{% block content %}

<div class="container">

{% for curr_notice in notices %}
<h3>{{ curr_notice.notice_title }}</h3>
<p>{{ curr_notice.notice_text }}</p>
<p><a href ="{% url 'delete' id=curr_notice.id %}" class="btn btn-info" role="button"> Löschen </a> </p>
{% endfor %}

<!--p><a href ="#" class="btn btn-info" role="button"> Neue Nachricht </a> </p-->
<!-- alter Button -> kein Verweis auf die neue view-Funktion namens "new()" -->

<p><a href ="{% url 'new' %}" class="btn btn-info" role="button"> Neue Nachricht </a> </p

</div>

{% endblock %}

+ 18
- 0
Django_Project/templates/posts/edit.html View File

@@ -0,0 +1,18 @@
{% extends "base.html" %}

{% block title %}
Eingabefenster für neuen instance von "Notice"
{% endblock %}


{% block content %}


<h1>Neue Nachricht</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Speichern</button>
</form>

{% endblock %}

+ 0
- 1
Python-Blockkurs/Pong.py View File

@@ -60,7 +60,6 @@ def doMovement():
image_rect.centery += move_vector[1]

if size_image[0]/2 <= image_rect.centerx <= width-(size_image[0]/2) and size_image[1]/2 <= image_rect.centery <= height-(size_image[1]/2):

pass
else:
if image_rect.centerx >= width-size_image[0]/2 or image_rect.centerx <= size_image[0]/2:

Loading…
Cancel
Save