简体   繁体   English

如何在Django项目中添加第二个表单?

[英]How can I add a second form to my Django Project?

I am new about Django , Python and in general web programming. 我是关于Django,Python和一般网络编程的新手。 I have a maybe a stupid problem but I am here trying to fix it from a week ago. 我有一个可能是一个愚蠢的问题,但我在这里试图修复它从一个星期前。

I have a Django project about food , I want two forms, first one is register a user(membresia or usuario) this works fine. 我有一个关于食物的Django项目,我想要两种形式,第一种是注册用户(membresia或usuario)这很好用。 and the other one is register a restaurant. 另一个是注册餐馆。

My Django version is 1.10 我的Django版本是1.10

models.py models.py

from django.db import models
from django.utils import timezone

# Create your models here.


class Membresia(models.Model):
    nombre = models.CharField(max_length=100)
    cedula = models.CharField(max_length=100)
    celular = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    ciudad = models.CharField(max_length=100)
    direccion = models.CharField(max_length=100)
    creado_en = models.DateTimeField(
                default=timezone.now)

class Restaurante(models.Model):
    nombre = models.CharField(max_length=100)
    nombre_responsable = models.CharField(max_length=100)
    direccion = models.CharField(max_length=100)
    celular = models.CharField(max_length=100)
    ciudad = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    creado_en = models.DateTimeField(
                default=timezone.now)

forms.py forms.py

from django import forms
from .models import Membresia, Restaurante





class membresiaform(forms.ModelForm):
    class Meta:
        model = Membresia
        fields = ('nombre', 'cedula','celular','email','ciudad','direccion')
        widgets = {
            'nombre':  forms.TextInput(attrs={'class': 'form-control','placeholder': 'Tu nombre'}),
            'cedula':  forms.TextInput(attrs={'class': 'form-control','placeholder': 'Tu número cédula'}),
            'celular': forms.TextInput(attrs={'class': 'form-control','placeholder': 'Tu número celular'}),
            'email':   forms.TextInput(attrs={'class': 'form-control','placeholder': 'Tu correo electrónico'}),
            'ciudad':  forms.TextInput(attrs={'class': 'form-control','placeholder': 'Ciudad donde vives'}),
            'direccion': forms.TextInput(attrs={'class': 'form-control','placeholder': 'Tu dirección'}),

        }

class restauranteform(forms.ModelForm):
    class Meta:
        model = Restaurante
        fields = ('nombre','nombre_responsable','direccion','celular','ciudad','email')
        widgets = {
            'nombre': forms.TextInput(attrs={'class': 'form-control','placeholder': 'Nombre del restaurante'}),
            'nombre_responsable': forms.TextInput(attrs={'class': 'form-control','placeholder': 'Nombre del representante '}),
            'direccion': forms.TextInput(attrs={'class': 'form-control','placeholder': 'Dirección del restaurante'}),
            'celular' : forms.TextInput(attrs={'class': 'form-control','placeholder': 'El celular de contácto'}),
            'ciudad': forms.TextInput(attrs={'class': 'form-control','placeholder': 'Ciudad de ubicación restaurante'}),
            'email': forms.TextInput(attrs={'class': 'form-control','placeholder': 'Correo eléctronico'}),

        }

views.py views.py

from django.shortcuts import render,render_to_response
from .forms import membresiaform, restauranteform
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.views.decorators.csrf import csrf_protect
from django.utils import timezone





# Create your views here.


def registrar_usuario_web(request):
    if "submit_us" in  request.POST:

        form = membresiaform(request.POST)
        if form.is_valid():
            form.creado_en = timezone.now()
            form.save()

            return HttpResponseRedirect('/')
    else:
        form = membresiaform()

    return render(request,'web/registrarusuario.html', {'form': form})

def registrar_restaurante_Web(request):
    if "submit_res" in  request.POST:
        form = restauranteform(request.POST)
        if form.is_valid():
            form.creado_en = timezone.now()
            form.save

            return HttpResponseRedirect('/')
    else:
        form = restauranteform()

    return render(request,'web/registrarrestaurante.html',{'form':form})

registrarrestaurante.html registrarrestaurante.html

{% block content %}
        <form action="/restaurante/registrar/" method="POST">
            <div class="post">
              <h1>Registrar restaurante</h1>
              {% csrf_token %}
                  {{ form.as_p }}
          {% endblock %}


          <div class="form-group">
              <input type="submit" name="submit_res" class="btn btn-submit" value="Enviar">
          </div>
      </form>

I can't save data in my Restaurante database. 我无法在我的Restaurante数据库中保存数据。 I didnt see errors, and the form is valid. 我没有看到错误,表格有效。 If I save data from Shell no problems. 如果我从Shell保存数据没有问题。 I searched questions here, but looks like everybody has the problem about to have two forms at same view, but I think this is not my problem, I someone can help me, I will be so grateful. 我在这里搜索问题,但看起来每个人都有问题要在同一个视图中有两种形式,但我认为这不是我的问题,我有人可以帮助我,我会非常感激。

I'm a rails developer, but I'm sure you can do this in Django as well. 我是一名rails开发人员,但我相信你也可以在Django中做到这一点。

I would add ID's to each submit button and forms then submit them using JS. 我会在每个提交按钮和表单中添加ID,然后使用JS提交它们。

<input type="submit" name="submit_res" class="btn btn-submit" value="Enviar" id='submit_res'>

then 然后

$('#submit_res').click(function () {
   $('#restauranteform').submit();  
});

$('#id_of_other_button').click(function () {
   $('#id_of_other_form').submit();  
});

The real reason that you can't save data into your table is because of this: 您无法将数据保存到表中的真正原因是:

form.save

it should be 它应该是

form.save()

Having solved that there are several other things to consider. 解决了其他几件事需要考虑。 For example, Shouldn't your members be allowed to login? 例如,您的会员不应该被允许登录吗? If yes, you are reinventing the wheel here. 如果是的话,你在这里重新发明轮子。 YOu could use django.contrib.auth.models.User as your user model and make use of one of the many tried and tested django authentication systems such as django-registration to get this task done with little or no effort. 你可以使用django.contrib.auth.models.User作为你的用户模型,并使用许多经过试验和测试的django身份验证系统之一,例如django-registration,只需很少或不费力就可以完成这项任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM