How to render the form from the template and see it in admin

0

I'm using Django 1.11.4,

root app

 url(r'^contacto/', include('contacto.urls'), name='contacto'),

contact_app / urls.py

from django.conf.urls import url, include
from contacto.views import contacto

urlpatterns = [
    url(r'^$', contacto, name='contacto'),
]

contact_app / forms.py

from django import forms

class formulario(forms.Form):
    nombre = forms.CharField(max_length=25)
    email = forms.EmailField(max_length=25)
    telefono = forms.CharField(max_length=15)
    mensaje = forms.CharField(max_length=300)

contact_app / views.py

from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from.models import Contacto


def contacto(request):
    contactos = Contacto.objects.all()
    return render(request, 'contacto.html', {'contactos': contactos})

contact_app / models.py

from django.db import models


class Contacto(models.Model):

    nombre = models.CharField(max_length=25)
    email = models.CharField(max_length=25)
    telefono = models.CharField(max_length=15)
    mensaje = models.CharField(max_length=300)

template / contacto.html

<body>
  <form action="/contacto/" method="post">{% csrf_token %}
    {{ form.as_table }}
    <input type="submit" value="Enviar">
</form>

When doing runserver I get this:

Terminal

[07 / May / 2018 23:35:42] "POST / contact / HTTP / 1.1" 200 472

I can not find the solution, I just want you to upload the form in the html, and that information can be seen in admin. Thanks in advance for the help

    
asked by Developer 08.05.2018 в 02:12
source

2 answers

2

Complementing the answer that they already gave you, I'll explain to you a little more thoroughly what you want and what you need.

The first thing is to start with your form, as you are recommended, it is better to inherit from the class forms.ModelForm because in this way, you can generate a form according to a class, and you will have at your disposal a method save to save directly in the table of the model that you assign to that form, leaving your form as follows:

forms.py

from django import forms
from .models import Contacto

class ContactoForm(forms.ModelForm):
    class Meta:
        model = Contacto
        fields = ('nombre', 'email', 'telefono', 'mensaje', )

To take into account, each field you add in the fields property must be contained within your model (in the models.py file, within the class). And they will be of the same type of data that you specify in the model.

The following is in your view, so that you can render the form well, it is important that you pass the context variables to the templates. The shortcut function render accepts 3 parameters, the first one: the request; Second: the path in string of the template to be rendered, and the third: are the variables that will be used by that template (the context). That is, any variable that you are going to pass to your template, must be inside this dictionary of variables, so if you want to pass your form and render it, you should do something like this:

views.py

from django.shortcuts import render
from .models import Contacto
from .forms import ContactoForm

def contacto(request):
    contactos = Contacto.objects.all()

    if request.method == 'POST':
        formulario = ContactoForm(data=request.POST)
        if formulario.is_valid():
            formulario.save()
    else:
        formulario = ContactoForm()

    return render(request, 'contacto.html', {'contactos': contactos, 'form': formulario})

This way you must render. And finally, if you want that form to be the same as that seen in the Django administrator. There is a file that is generated automatically when we create an app with manage.py and that file is called admin.py where we are supposed to take the classes and functions related to the admin for our app.

For your form to be reflected in the admin, make sure you have this in the admin

admin.py

from . import models, forms
from django.contrib import admin

class ContactoAdmin(admin.ModelAdmin):
    form = forms.ContactoForm

admin.site.register(models.Contacto, ContactoAdmin)

This way your form should also be seen in the form when you do something with the administrator

EDITO

I add in the view how you should treat your form, so that when it arrives by the POST method it validates it and if everything is correct, it saves it in the database so that later you can see it from the admin

    
answered by 08.05.2018 / 16:00
source
0

From what I'm seeing at no point are you passing the variable form.

Change the form.Form by form.ModelForm in your class form. There you can use the class Meta and include the model you want to use.

You should also load the form in your contact function and include it in the render as you are including contacts.

If you want to see that information in /admin/ , you should create a class ModelAdmin in your admin.py file

    
answered by 08.05.2018 в 09:44