一页上的多个表单上的Django值错误

时间:2016-08-20 07:51:54

标签: django

我在一个模板上有一个表单,每次我点击提交按钮,我都会被重定向到一个抛出值错误的页面。我尝试过在网上寻求帮助但没有运气,另一种形式在模板上完全正常,它只是文档形式。我在网上搜索过在一个模板上处理多个表单然后遇到的一组代码,结构类似于此。但由于一些奇怪的原因,我不能让它工作,我事件试图为我的第二个形式写一个单独的视图,没有运气。我对django还很新。

def modify_land(request, pk):
    # Query Sets
    land = get_object_or_404(Land, pk=pk)
    land_use_types = LandType.objects.all()
    countries = Country.objects.all()
    province_states = ProvinceState.objects.all()
    counties = County.objects.all()
    lld_types = LLDType.objects.all()
    land_owners = LandOwner.objects.all()
    tenants = Tenant.objects.all()
    documents = Document.objects.all()
    document_types = DocumentType.objects.all()
    document_statuses = DocumentStatus.objects.all()
    partners = Partner.objects.all()
    events = Event.objects.all()
    # Forms
    doc_form = DocumentForm()
    # Modify Land
    if request.method == "POST":
        form = LandForm(request.POST, instance=land)
        if form.is_valid():
            land = form.save(commit=False)
            land.creator = request.user
            land.modified_date = timezone.now()
            land.save()
            messages.success(request, 'Object has Been Modified')
            return redirect(lands_home)
        else:
            messages.error(request, 'Object Has Not Been Modified')
    else:
        form = LandForm(instance=land)
    # Create Relationship to Document
    if request.method == "POST" and 'land_rel_doc' in request.POST:
        doc_form = DocumentForm(request.POST, request.FILES)
        if doc_form.is_valid():
            document = form.save(commit=False)
            document.creator = request.user
            document.created_date = timezone.now()
            document.save()
            messages.success(request, 'Object Has Been Created')
        else:
            messages.error(request, 'Object Has Not Been Created')
    else:
        doc_form = DocumentForm()
    return render(request,
                  'process_lands_management/modify_land.html',
                  {'form': form,
                   'doc_form': doc_form,
                   'land': land,
                   'land_use_types': land_use_types,
                   'countries': countries,
                   'province_states': province_states,
                   'counties': counties,
                   'lld_types': lld_types,
                   'land_owners': land_owners,
                   'tenants': tenants,
                   'documents': documents,
                   'document_types': document_types,
                   'document_statuses': document_statuses,
                   'partners': partners,
                   'events': events,
                   'title': 'Modify Land'})

错误:

Internal Server Error: /process_lands_management/modify_land/1/
Traceback (most recent call last):
  File "/home/python/.virtualenvs/django_imax/lib/python3.4/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/python/.virtualenvs/django_imax/lib/python3.4/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/python/.virtualenvs/django_imax/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/python/.virtualenvs/django_imax/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/python/python/django_imax/django_imax/process_lands_management/views.py", line 114, in modify_land
    document = form.save(commit=False)
  File "/home/python/.virtualenvs/django_imax/lib/python3.4/site-packages/django/forms/models.py", line 443, in save
    'created' if self.instance._state.adding else 'changed',
ValueError: The Land could not be changed because the data didn't validate.

1 个答案:

答案 0 :(得分:0)

将第setValue()行更改为document = form.save(commit=False)