首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >CreateView在提交时不保存,但在UpdateView上

CreateView在提交时不保存,但在UpdateView上
EN

Stack Overflow用户
提问于 2019-03-05 16:42:49
回答 1查看 126关注 0票数 1

我已经在这个问题上挠头两天了,希望有人能帮我找出我遗漏了什么。

我以前可以提交,但我改变了一些东西,我不知道是什么。

当我尝试添加一个新的客户端时,页面只是重新加载,什么也不做。

但是,当我去编辑一个已经创建的文件时(使用管理控制台),它可以正常工作。

models.py

代码语言:javascript
代码运行次数:0
运行
复制
from django.urls import reverse
from django.db.models import CharField
from django.db.models import DateTimeField
from django.db.models import FileField
from django.db.models import IntegerField
from django.db.models import TextField
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth import get_user_model
from django.contrib.auth import models as auth_models
from django.db import models as models
from django.contrib.auth.models import User
from django_extensions.db import fields as extension_fields

FINANCING_TYPE = (
    ('LoanPal', 'LoanPal'),
    ('Renew', 'Renew'),
    ('GreenSky', 'GreenSky'),
    ('Cash/Card/Check', 'Cash/Card/Check'),
    ('Other', 'Other - Please Note'),
)

ROOF_TYPE = (
    ('Cement Flat', 'Cement Flat'),
    ('Cement S/W', 'Cement S/W'),
    ('Composite', 'Composite'),
    ('Clay', 'Clay'),
    ('Metal', 'Metal'),
    ('Other', 'Other - Please Note'),
)

JOB_STATUS = (
    ('New', 'New'),
    ('Sent to Engineer', 'Sent to Engineer'),
    ('Installer', 'Installer'),
    ('Completed', 'Completed'),
)


class Client(models.Model):

    # Fields
    sales_person = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
    job_status = models.CharField(max_length=30, choices=JOB_STATUS, default="New")
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    street_address = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    zipcode = models.CharField(max_length=10)
    phone_number = models.CharField(max_length=16)
    email_address = models.CharField(max_length=100)
    financing_type = models.CharField(max_length=30, choices=FINANCING_TYPE)
    contract_amount = models.IntegerField()
    contract_pdf = models.FileField(upload_to="upload/files/contracts/")
    electric_bill = models.FileField(upload_to="upload/files/electric_bills/")
    roof_type = models.CharField(max_length=30, choices=ROOF_TYPE)
    edge_of_roof_picture = models.ImageField(verbose_name="Picture of Roof Edge", upload_to="upload/files/edge_of_roof_pictures/")
    rafter_picture = models.ImageField(verbose_name="Picture of Rafter/Truss", upload_to="upload/files/rafter_pictures/")
    biggest_breaker_amp = models.IntegerField()
    electric_panel_picture = models.ImageField(verbose_name="Picture of Electrical Panel", upload_to="upload/files/electric_panel_pictures/")
    electric_box_type = models.CharField(verbose_name="Top or bottom fed electric box (is there an overhead line coming in? Can you see where the electric line comes in?)", max_length=100)
    main_panel_location = models.CharField(verbose_name="Main panel location (looking at house from street)", max_length=100)
    additional_notes = models.TextField(verbose_name="Any Additional Notes?", blank=True)
    customer_informed = models.BooleanField(verbose_name="Informed customer plans and placards will be mailed to them and make them available install day")
    class Meta:
        ordering = ('-created',)

    def get_absolute_url(self):
        return reverse('sales_client_detail', args=[str(self.id)])

    def __str__(self):
        return self.street_address

views.py

代码语言:javascript
代码运行次数:0
运行
复制
from django.views.generic import DetailView, ListView, UpdateView, CreateView
from .models import Client
from .forms import ClientForm
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy

class ClientListView(LoginRequiredMixin,ListView):
    model = Client
    def get_queryset(self):
        if not self.request.user.is_staff:
            return Client.objects.filter(sales_person=self.request.user)
        else:
            return Client.objects.all()

class ClientCreateView(LoginRequiredMixin,CreateView):
    form_class = ClientForm
    model = Client

class ClientDetailView(LoginRequiredMixin,DetailView):
    model = Client

#    def get_object(self):
#        if not self.request.user.is_staff:
#            return get_object_or_404(Client, sales_person=self.request.user)
#        else:
#            queryset = self.get_queryset()
#            obj = get_object_or_404(queryset)
#            return obj



class ClientUpdateView(LoginRequiredMixin,UpdateView):
    model = Client
    form_class = ClientForm

forms.py

代码语言:javascript
代码运行次数:0
运行
复制
from django import forms
from .models import Client


class ClientForm(forms.ModelForm):
    class Meta:
        model = Client
        fields = ['first_name', 'last_name', 'street_address', 'city', 'zipcode',
                  'phone_number', 'email_address', 'financing_type', 'contract_amount',
                  'contract_pdf', 'electric_bill', 'roof_type', 'edge_of_roof_picture',
                  'rafter_picture', 'biggest_breaker_amp', 'electric_panel_picture',
                  'electric_box_type', 'main_panel_location', 'additional_notes', 'customer_informed']

urls.py

代码语言:javascript
代码运行次数:0
运行
复制
from django.urls import path, include

from . import views



urlpatterns = (
    # urls for Client
    path('clients/', views.ClientListView.as_view(), name='sales_client_list'),
    path('client/create/', views.ClientCreateView.as_view(), name='sales_client_create'),
    path('client/view/<int:pk>', views.ClientDetailView.as_view(), name='sales_client_detail'),
    path('client/update/<int:pk>', views.ClientUpdateView.as_view(), name='sales_client_update'),
)

client_form.html

代码语言:javascript
代码运行次数:0
运行
复制
{% extends "base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}

<form method="POST">
{% csrf_token %}
{{form|crispy}}
<button class="btn btn-primary" type="submit">Submit</button>
</form>
{% endblock %}
EN

回答 1

Stack Overflow用户

发布于 2019-03-06 01:31:06

我通过irc.Freenode.net在#Django上获得了GinFuyou的帮助

问题是我需要在client_form.html上使用enctype=“multipart/form-data

整行应为

代码语言:javascript
代码运行次数:0
运行
复制
<form enctype="multipart/form-data" method="POST">

这是由于已将文件上载到表单。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54998602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档