diff --git a/lostplaces/lostplaces_app/forms.py b/lostplaces/lostplaces_app/forms.py index fc99edc..734e77d 100644 --- a/lostplaces/lostplaces_app/forms.py +++ b/lostplaces/lostplaces_app/forms.py @@ -1,9 +1,8 @@ from django import forms from django.contrib.auth.forms import UserCreationForm, UserChangeForm -from .models import Explorer +from .models import Explorer, Place class ExplorerCreationForm(UserCreationForm): - class Meta: model = Explorer fields = ('username', 'email') @@ -13,3 +12,9 @@ class ExplorerChangeForm(UserChangeForm): class Meta: model = Explorer fields = ('username', 'email') + +class PlaceForm(forms.ModelForm): + class Meta: + model = Place + fields = '__all__' + exclude = ['submitted_by'] diff --git a/lostplaces/lostplaces_app/migrations/0002_place_submitted_by.py b/lostplaces/lostplaces_app/migrations/0002_place_submitted_by.py new file mode 100644 index 0000000..8fc80ff --- /dev/null +++ b/lostplaces/lostplaces_app/migrations/0002_place_submitted_by.py @@ -0,0 +1,20 @@ +# Generated by Django 3.0.8 on 2020-07-29 16:29 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('lostplaces_app', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='place', + name='submitted_by', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/lostplaces/lostplaces_app/models.py b/lostplaces/lostplaces_app/models.py index 270f12f..42d3bae 100644 --- a/lostplaces/lostplaces_app/models.py +++ b/lostplaces/lostplaces_app/models.py @@ -18,6 +18,7 @@ class Explorer(AbstractUser): # Place defines a lost place (location, name, description etc.). class Place (models.Model): name = models.CharField(max_length=50) + submitted_by = models.ForeignKey(Explorer, on_delete=models.SET_NULL, null=True, blank=True, related_name='places') location = models.CharField(max_length=50) latitude = models.FloatField() longitude = models.FloatField() diff --git a/lostplaces/lostplaces_app/templates/create_place.html b/lostplaces/lostplaces_app/templates/create_place.html new file mode 100644 index 0000000..dab22be --- /dev/null +++ b/lostplaces/lostplaces_app/templates/create_place.html @@ -0,0 +1,15 @@ +{% extends 'global.html'%} +{% load static %} + +# {% block title %}Place erstellen{% endblock %} + +{% block maincontent %} + +

Place erstellen

+
+ {% csrf_token %} + {{ form.as_p }} + +
+ +{% endblock maincontent %} \ No newline at end of file diff --git a/lostplaces/lostplaces_app/urls.py b/lostplaces/lostplaces_app/urls.py index 2eb4107..baf9b1a 100644 --- a/lostplaces/lostplaces_app/urls.py +++ b/lostplaces/lostplaces_app/urls.py @@ -1,9 +1,10 @@ from django.urls import path -from .views import hello_world, place_detail_view, place_list_view, SignUpView +from .views import hello_world, place_detail_view, place_list_view, SignUpView, PlaceEditView urlpatterns = [ path('hello_world/', hello_world), # You know what this is :P path('signup/', SignUpView.as_view(), name='signup'), path('place//', place_detail_view, name='place_detail'), - path('places/', place_list_view) + path('place/create/', PlaceEditView.as_view(), name='place_create'), + path('place/', place_list_view) ] \ No newline at end of file diff --git a/lostplaces/lostplaces_app/views.py b/lostplaces/lostplaces_app/views.py index 3656295..3ab95e8 100644 --- a/lostplaces/lostplaces_app/views.py +++ b/lostplaces/lostplaces_app/views.py @@ -1,8 +1,9 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect from django.urls import reverse_lazy from django.views.generic.edit import CreateView +from django.views import View -from .forms import ExplorerCreationForm +from .forms import ExplorerCreationForm, PlaceForm from .models import Place # Create your views here. @@ -20,3 +21,18 @@ def place_detail_view(request, pk): def hello_world(request): return render(request, 'hello_world.html', {'text':'Hello World!'}) + +class PlaceEditView(View): + def get(self, request, *args, **kwargs): + place_form = PlaceForm() + return render(request, 'create_place.html', {'form':place_form}) + def post(self, request, *args, **kwargs): + place_form = PlaceForm(request.POST) + if place_form.is_valid() == True: + instance = place_form.save(commit=False) + # Save logged in user as "submitted_by" + instance.submitted_by = request.user + instance.save() + return redirect(reverse_lazy('place_detail', kwargs={'pk':instance.pk})) + else: + return render(request, 'create_place.html', {'form':place_form})