Django 表单发布不起作用 - Django 只是再次渲染页面

基本上我已经建立了一个表格来创建组织。当我点击“保存”按钮时,它只是再次呈现页面 - POST 不起作用。


请参阅下面我的代码:


models.py


from django.db import models

from accounts.models import User

from datetime import datetime, date


#// ------------ FUNCTIONS -------------//


# Generate Organisation IDs for each organisation

def org_id_generate():

    last_org = Organization.objects.all().order_by('org_id').last()

    if not last_org:

        return 'ORG_001'

    else:

        last_org_id = last_org.org_id

        number_in_id = int(last_org_id[4:7])

        new_number_in_id = number_in_id + 1

        new_org_id = 'ORG_' + str(new_number_in_id).zfill(3)

        return new_org_id


#// ------------ MODELS -------------//


class Organization(models.Model):


    org_id = models.CharField(primary_key=True, max_length=7, default=org_id_generate, editable=False)

    organization_code = models.CharField(max_length=20)

    company_name = models.CharField(verbose_name="Company Name", max_length=60)

    legal_name = models.CharField(verbose_name="Legal Name", max_length=100)

    industry_distribution = models.BooleanField(verbose_name="Distribution", default=False)

    industry_education = models.BooleanField(verbose_name="Education", default=False)

    industry_healthcare = models.BooleanField(verbose_name="Healthcare", default=False)

    industry_manufacturing = models.BooleanField(verbose_name="Manufacturing", default=False)

    industry_retail = models.BooleanField(verbose_name="Retail", default=False)

    industry_services = models.BooleanField(verbose_name="Services", default=False)

    business_registration_no = models.CharField(verbose_name="Business Registration Number", max_length=15, blank=True)

    vat_registration_no = models.CharField(verbose_name="VAT Registration Number", max_length=15, blank=True)


慕森王
浏览 123回答 2
2回答

RISEBY

我没有找到任何代码来显示 html 中的错误。根据views中的函数,如果表单无效,则使用表单渲染页面。尝试在html文件中添加{{form.errors}}看看是否有错误?

侃侃无极

我设法解决了它。views.py@login_requireddef organization_add(request):    if request.method == 'POST':        form = OrganizationAddForm(request.POST)        if form.is_valid():            form.organization_code = form.cleaned_data['organization_code']            form.company_name = form.cleaned_data['company_name']            form.legal_name = form.cleaned_data['legal_name']            form.business_registration_no = form.cleaned_data['business_registration_no']            form.vat_registration_no = form.cleaned_data['vat_registration_no']            form.industry_distribution = form.cleaned_data['industry_distribution']            form.industry_education = form.cleaned_data['industry_education']            form.industry_healthcare = form.cleaned_data['industry_healthcare']            form.industry_manufacturing = form.cleaned_data['industry_manufacturing']            form.industry_retail = form.cleaned_data['industry_retail']            form.industry_services = form.cleaned_data['industry_services']            form.effective_start_date = form.cleaned_data['effective_start_date']            form.effective_end_date = form.cleaned_data['effective_end_date']                        org = form.save(commit=False)            org.created_by = request.user            org.last_updated_by = request.user            org.save()            return redirect('organizations_settings')    else:        form = OrganizationAddForm()    return render(request, 'settings/add_organization.html', {'form': form})问题是它无法捕获“创建者”和“上次更新者”字段的用户电子邮件。这是通过使用以下方法解决的:org = form.save(commit=False)org.created_by = request.userorg.last_updated_by = request.user
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python