Consolidated Edit View
This commit is contained in:
@@ -5,6 +5,6 @@ from . import views
|
||||
urlpatterns = [
|
||||
path('', views.root, name="root"),
|
||||
path('list', views.VenueListView.as_view(), name="venues list"),
|
||||
path('edit/<int:id>', views.edit, name="edit venue"),
|
||||
path('edit', views.edit, name="edit venue")
|
||||
path('edit/<int:id>', views.VenueEditView.as_view(), name="edit venue"),
|
||||
path('edit', views.VenueEditView.as_view(), name="edit venue")
|
||||
]
|
||||
@@ -3,7 +3,7 @@ from django.http import HttpResponse, HttpResponseBadRequest
|
||||
from django.urls import reverse
|
||||
from .models import Venue
|
||||
from .forms import VenueForm
|
||||
from lib.views import BenchcoachListView
|
||||
from lib.views import BenchcoachListView, BenchcoachEditView
|
||||
|
||||
def root(request):
|
||||
return redirect('/venues/list')
|
||||
@@ -14,30 +14,8 @@ class VenueListView(BenchcoachListView):
|
||||
list_url = 'venues list'
|
||||
page_title = "Venues"
|
||||
|
||||
def edit(request, id=0):
|
||||
# if this is a POST request we need to process the form data
|
||||
if request.method == 'POST':
|
||||
# create a form instance and populate it with data from the request:
|
||||
if id:
|
||||
instance = get_object_or_404(Venue, id=id)
|
||||
form = VenueForm(request.POST or None, instance=instance)
|
||||
else:
|
||||
form = VenueForm(request.POST or None)
|
||||
# check whether it's valid:
|
||||
if form.is_valid():
|
||||
# process the data in form.cleaned_data as required
|
||||
# ...
|
||||
# redirect to a new URL:
|
||||
new_venue, did_create = Venue.objects.update_or_create(pk=id, defaults=form.cleaned_data)
|
||||
return render(request, 'success.html', {'call_back_url':reverse('venues list'), 'id':new_venue.id}, status=201 if did_create else 200)
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
# if a GET (or any other method) we'll create a blank form
|
||||
else:
|
||||
if id:
|
||||
instance = get_object_or_404(Venue, id=id)
|
||||
form = VenueForm(request.POST or None, instance=instance)
|
||||
else:
|
||||
form = VenueForm
|
||||
|
||||
return render(request, 'edit.html', {'form': form, 'id': id, 'call_back': 'edit venue'})
|
||||
class VenueEditView(BenchcoachEditView):
|
||||
Model = Venue
|
||||
edit_url = 'edit venue'
|
||||
list_url = 'venues list'
|
||||
Form = VenueForm
|
||||
Reference in New Issue
Block a user