Thursday, December 28, 2017

RE: Possible bug Django 2.0 CreateView generic CBV

The problem is in your HTML template.

Your form tag really only has to be this (the action attribute should be a URL; otherwise, it assumes the current URL):

 

<form method="POST">

 

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Steven Meyer
Sent: Thursday, December 28, 2017 7:44 PM
To: Django users
Subject: Possible bug Django 2.0 CreateView generic CBV

 

I hope I've got the right group this time. I originally posted it on Django core membership but Tim Graham emailed me to say it was the wrong group.

 

I am very new to Django - been at it for five weeks. Not sure whether this is a bug or my own stupidity.

 

Here is my model in an app called gcbv (for generic class-based view)

 

from django.db import models

from core.models import TimeStampModel

from django.urls import reverse

# Create your models here.

 

class Vehicle(TimeStampModel):

            maker = models.CharField(max_length=100)

            model_year = models.IntegerField()

            vehicle_type = models.CharField(max_length=100)

            slug = models.SlugField(max_length=100, unique=True)

            vehicle_model = models.CharField(max_length=100)

            website = models.URLField(max_length=100, blank=True)

            email = models.EmailField(max_length=100, blank=True)

            notes = models.TextField(blank=True, default='')

           

           

            def __str__(self):

                        x = self.maker + ' ' + self.vehicle_model

                        return x

           

 

And here are the URLs

 

from django.contrib import admin

from django.urls import path, include

from django.conf.urls import url

from . import views

from django.urls import reverse

#from django.views.generic.base import TemplateView

 

app_name = 'gcbv'

 

urlpatterns = [

    path('sub1/', views.SubView.as_view(), name='sub1'),

    path('vehicle_list/', views.VehicleListView.as_view(),

        name = 'vehicle_list'),

    path('vehicle/<str:slug>/', 

        views.VehicleDetailView.as_view(), 

        name='vehicle_detail'),

    path('vehicle/create', views.VehicleCreateView.as_view(),

        name='vehicle_create'),    

    path('', views.IndexTemplateView.as_view(), name='index'),

]

 

And here is the relevant view

 

class VehicleCreateView(CreateView):

            model = Vehicle

            fields = ['maker', 'model_year', 'vehicle_type', 'slug',

                'vehicle_model', 'website', 'email', 'notes']

            labels = {'maker':'Maker', 'model_year':'Year', 

                'vehicle_type':'Type', 'vehicle_model':'Model',

                'website':'Website', 'email':'Email', 'notes':'Notes'}

 

 

Here is the template:

 

{% extends "core/base.html" %}

{% block body_block %}

<h1>Vehicle Create for GCBV</h1>

 

<form action="POST" action="">

 

  {% csrf_token %}

  {{ form.as_p }}

  <button name="submit" class="btn btn-primary">Submit</button>

</form>

 

<h1>End Vehicle Create for GCBV</h1>

{% endblock %}

 

I've attached screenshots of:

 

--The rendered form template

 

--What happens after I click submit

 

It looks as if the data is not being saved to the database.

 

If I add the same data from admin everything works fine. I've attached another screenshot showing that VehicleDetailView has found the relevant template and rendered the information.

 

Any help would be greatly appreciated.

 

NB: This all worked fine when I was using function views and regex instead of path.

 

 

 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5aa037bb-25d5-442d-b1a6-b1bbc558dead%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment