Hola me está sucediendo algo interesante: No consigo que me funcione el método "Post" con un formulario de creación en Django. Lo he intentando tanto con vistas basadas en clase como en funciones. Nota si creo desde el administrador, alli funciona perfectamente y se lista.
En el código aparecen ambas opciones, puedo listar sin problemas, pero no me inserta en la bd, estoy revisando desde network y todo el tiempo mantiene GET como cabecera, ademas y tampoco me hace la redireccion, supongo que al no funcionar el post, el metodo save que incorpora el CreateView no llega a ser llamado
Muestro el código y adelanto desde ya las gracias por la ayuda.
Models.py
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import User
from django.utils.text import slugify
from django.templatetags.static import static
class Section(models.Model):
# Sections will be the differents area's enterprise are divided
section_name = models.CharField(max_length=64)
phone = models.CharField(max_length=12, blank=True, null=True)
email = models.EmailField(blank=True, null=True)
web = models.URLField(max_length=100, blank=True,null=True)
postal_code = models.CharField(max_length=10, blank=True, null=True)
country = models.CharField(max_length=100, blank=True, null=True)
address = models.CharField(max_length=200, blank=True, null=True)
section_description = models.TextField(blank=True, null=True)
class Meta:
ordering = ('section_name',)
def __str__(self):
return self.section_name
def get_absolute_url(self):
return reverse('all_sections.html', kwargs={'pk':self.id})
Section.py
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.templatetags.static import static
from django.views.generic import ListView
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from django.views.decorators.csrf import csrf_protect
from django.utils.decorators import method_decorator
from backoffice.forms import SectionForm
from backoffice.models import Section
from django.urls import reverse, reverse_lazy
#basandome en clases
class SectionCreate(CreateView):
model = Section
form_class = SectionForm
template_name = 'sections/new_section.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['tittle'] = 'Sections Forms'
context['table_tittle'] = 'New Section'
context['table_subtittle'] = 'Add here your new sectionss'
return context
def get_success_url(self):
return reverse('all_sections')
#basandome en una funcion
def crearsection(request):
if request.method == 'GET':
form = SectionForm()
context = {
'form':form
}
else:
form =SectionForm(request.POST)
context ={
'form':form
}
if form.is_valid():
form.save()
return redirect('all_sections')
return render(request, 'sections/new_section.html', context)
urls.py
from django.contrib import admin
from django.views.generic import TemplateView
from django.urls import path
from backoffice.views.section import SectionCreate, SectionList, SectionUpdate, SectionDelete
path('backoffice/new_section',
SectionCreate.as_view(), name="new_section"),
#path('backoffice/new_section', backoffice.views.section.crear_section, name="crear_section"),
new_section.html
{% block form %}
<form action:" " method:"POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p}}
<div class="card-footer">
<input type="submit" value="Add New" class="btn btn-primary btn-flat">
<i class="fas fa fa-save"></i>
</input>
</div>
{% endblock %}
```