Django表单提交

时间:2015-04-21 19:39:43

标签: python django forms django-models django-forms

我对Django 1.8中的表单提交有疑问。问题在于提交表单的逻辑。当在模板中执行两个表单时,第一个表单正常保存到数据库,但其他表单不会保存。我不知道我在表单提交方面做错了什么。或者也许这是forms.py中的一个问题。请看下面的代码:

models.py

class IzibiziUser(AbstractEmailUser):

    full_name  = models.CharField(max_length=50, blank=True, db_index=True)
    first_name = models.CharField(max_length=25, blank=True)
    last_name  = models.CharField(max_length=35, blank=True)

    role_id         = models.ForeignKey('Roles', null=True, blank=True)
    organization_id = models.ForeignKey('IzibiziOrganization', null=True, blank=True)


    @property
    def __str__(self):
        return self.full_name

class OrganizationInfo(models.Model):
    id = models.AutoField(primary_key=True, unique=True)
    oib = models.CharField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    address = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    postal_code = models.CharField(max_length=255)
    country = models.CharField(max_length=255)
    phone = models.CharField(max_length=255)
    email = models.EmailField()
    iban = models.CharField(max_length=255)
    webpage = models.URLField()
    delivery_address = models.CharField(max_length=255)
    delivery_city = models.CharField(max_length=255)
    delivery_postal_code = models.CharField(max_length=255)
    delivery_country = models.CharField(max_length=255)

    def __str__(self):
        return str(self.id)

class OutgoingInvoice(models.Model):
    id = models.AutoField(primary_key=True)
    invoice_number = models.IntegerField(blank=True, null=True)
    date = models.DateField(blank=True, null=True)
    time = models.TimeField(blank=True, null=True)
    place = models.CharField(max_length=255, blank=True, null=True)
    status = models.CharField(max_length=255, blank=True, null=True)
    due_date = models.DateField(blank=True, null=True)
    total_invoice_amount = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
    vat_amount = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
    invoice_amount = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
    discount = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
    user_id = models.ForeignKey('IzibiziUser', blank=True, null=True)
    organization_id = models.ForeignKey('IzibiziOrganization', blank=True, null=True)
    customer_id = models.ForeignKey('OrganizationInfo', blank=True, null=True)

    def __str__(self):
        return str(self.id)

forms.py

# New Outgoing Invoice
class InsertNewCustomer(forms.ModelForm):
    name        = forms.RegexField(regex=r'\w+',  label=_('Klijent'),widget   = forms.TextInput({'class':'form-control', 'placeholder':'Klijent'}))
    oib         = forms.RegexField(regex=r'\d.+', label=_('OIB'),widget  = forms.TextInput({'class':'form-control', 'placeholder':'OIB'} ))
    address     = forms.RegexField(regex=r'\w+',  label=_('Adresa'), widget  = forms.TextInput({'class':'form-control', 'placeholder':'Adresa'}))
    city        = forms.RegexField(regex=r'\w+',  label=_('Grad'), widget  = forms.TextInput({'class':'form-control', 'placeholder':'Grad'}))
    postal_code = forms.RegexField(regex=r'\w+',  label=('Poštanski broj'),widget  = forms.TextInput({'class':'form-control', 'placeholder':'Poštanski broj'}))


    @property
    def clean_form(self):
        try:
            obj = OrganizationInfo.object.get(oib__iexact=self.cleaned_data['oib'])
        except OrganizationInfo.DoesNotExist:
            return self.clean_form

    class Meta:
        model = OrganizationInfo
        fields = ('name', 'oib', 'address', 'city', 'postal_code',)
        exclude = (
            'country', 'phone', 'email', 'iban', 'webpage', 'delivery_address', 'delivery_city', 'delivery_postal_code',
            'delivery_country', )


class OutNewInovice(forms.ModelForm):


    date            = forms.RegexField(regex=r'\w+', label=('Datum računa'), widget  = forms.TextInput({'class':'form-control date-now', 'placeholder':'Datum računa'}))
    due_date        = forms.DateField(label=('Datum dospjeća'), widget  = forms.TextInput({'class':'form-control select_date', 'placeholder':'Datum dospjeća'}))
    time            = forms.RegexField(regex=r'\w+', label=_('Vrijeme'), widget  = forms.TextInput({'class':'form-control time-now', 'placeholder':'Vrijeme'}))
    status          = forms.ChoiceField(widget = forms.Select({'class':'selectpicker'}),choices = ([('1','Neplaćeno'), ('2','Plaćeno')]), initial='1', required = True, )
    place           = forms.RegexField(regex=r'\w+',  label=_('Mjesto'), widget  = forms.TextInput({'class':'form-control', 'placeholder':'Mjesto'}))
    invoice_number  = forms.RegexField(regex=r'\w+', label=('Broj računa'), widget  = forms.TextInput({'class':'form-control', 'placeholder':'Broj računa'}))

    #status.forms.widget.attrs[{'class': 'selectpicker'}]

    class Meta:
        model = OutgoingInvoice

        fields = ('date', 'due_date', 'time', 'status', 'place', 'invoice_number',)
        exclude = ('total_invoice_amount', 'vat_amount', 'vat_amount', 'invoice_amount', 'discount', )

views.py

@login_required
@csrf_protect
def NewOutgoingInvoice(request):
    context = RequestContext(request)
    template = "novi_izlazni_racun.html"

    user_pk = request.user.id
    org_name = OrganizationInfo.objects.filter(id=user_pk).values('name')[0]
    wtf = OrganizationInfo.objects.filter(id=user_pk).values('id')

    if request.method == 'POST':
        form1 = InsertNewCustomer(request.POST, prefix='form1')
        form2 = OutNewInovice(request.POST, prefix='form2')
        if all([form1.is_valid(), form2.is_valid()]):
            a = form1.save()
            b = form2.save()
            return HttpResponseRedirect('/novi_izlazni_racuni/')
    else:
        form1 = InsertNewCustomer(prefix='form1')
        form2 = OutNewInovice(prefix='form2')

    variables =  {
        'name': org_name,
        'form1': form1,
        'form2': form2,
    }
    return render(request, template, variables,context_instance=RequestContext(request))

模板

<form  action="/novi_izlazni_racuni/"  method="POST" role="form" >
    {% csrf_token %}
<div class="panel-body">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label class="control-label">{{ form1.name.label }} <span class="example1">*</span></label>
                {{ form1.name }}
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label class="control-label">{{ form1.oib.label }} <span class="example1">*</span></label>
                {{ form1.oib }}
            </div>
        </div>
    </div>
    <br>
    <div class="row">
        <div class="col-md-4 mar-btm">
            <label class="control-label">{{ form1.address.label }} <span class="example1">*</span></label>
            {{ form1.address }}
        </div>
        <div class="col-md-4 mar-btm">
            <label class="control-label">{{ form1.city.label }} <span class="example1">*</span></label>
            {{ form1.city }}
        </div>
        <div class="col-md-4 mar-btm">
            <label class="control-label">{{ form1.postal_code.label }} <span class="example1">*</span></label>
            {{ form1.postal_code }}
        </div>
    </div>


    <div class="panel-heading">
        <h3 class="panel-title" style="margin-left: -15px;">DETALJI RAČUNA</h3>
        <div class="row bord-top" style="opacity:1;"></div> <!--Horizontal line-->
    </div>
    <br>
    <div class="row">
        <div class="col-md-4 mar-btm">
            <label class="control-label">{{ form2.date.label }} <span class="example1">*</span></label>
            {{ form2.date }}
        </div>
        <div class="col-md-4 mar-btm">
            <label class="control-label">{{ form2.time.label }} <span class="example1">*</span></label>
            {{ form2.time }}
        </div>
        <div class="col-md-4 mar-btm">
            <label class="control-label">{{ form2.place.label }} <span class="example1">*</span></label>
            {{ form2.place }}
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 mar-btm">
            <label class="control-label">{{ form2.due_date.label }} <span class="example1">*</span></label>
            {{ form2.due_date }}
        </div>
        <div class="col-md-4 mar-btm">
            <p style="margin:0 0 5.1px;">{{ form2.status.label }} <span class="example1">*</span></p>
            {{ form2.status }}

        </div>
        <div class="col-md-4 mar-btm">
            <label class="control-label">{{ form2.invoice_number.label }}</label>
            {{ form2.invoice_number }}
        </div>
    </div>

新插入后的模型OrganizationInfo数据库中的结果不存在它工作。但插入OutgoingInvoice将无法正常工作,这是一个问题。所以,如果你能给我一些例子。

0 个答案:

没有答案