Compare commits

...

2 Commits

7 changed files with 84 additions and 6 deletions

View File

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

View File

@ -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),
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 3.0.8 on 2020-07-29 18:22
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('lostplaces_app', '0002_place_submitted_by'),
]
operations = [
migrations.AlterField(
model_name='place',
name='submitted_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='places', to=settings.AUTH_USER_MODEL),
),
]

View File

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

View File

@ -0,0 +1,15 @@
{% extends 'global.html'%}
{% load static %}
# {% block title %}Place erstellen{% endblock %}
{% block maincontent %}
<h2>Place erstellen</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Abschicken</button>
</form>
{% endblock maincontent %}

View File

@ -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/<int:pk>/', 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)
]

View File

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