简体   繁体   中英

Django Error form submission

I have an app that let you create a profile. One of my app feature is it's let you edit your name and upload a image.

The problem is the user cannot submit the image unless he type his name.How can I fix this page to make it so If the user submit an image but doesn't submit a name . He will still have his old name or if he doesn't submit an image and changes his name . He will still have his old picture?

I tried adding blank=True and null = False , null = True but doesn't seem to do the job

My models.py

class Person(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100,null=True,blank=False)
    image = models.FileField(upload_to="images/")

    def __unicode__(self):
        return self.name

My forms.py

 class PersonForm(forms.ModelForm):
    class Meta:
        model = Person
        fields = ('image','name',)

My views.py

def Display(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('world:LoginRequest'))

    if request.method == 'POST':
        form = PersonForm(request.POST, request.FILES)
        if form.is_valid():
            person = Person.objects.get(user=request.user)
            person.image = form.cleaned_data['image']
            person.name = form.cleaned_data['name']
            person.save() 
    return render(request,'edit.html',{'form': PersonForm()})

在此输入图像描述

class Person(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100, blank=True)
    image = models.FileField(upload_to="images/", blank=True)

    def __unicode__(self):
        return self.name

def Display(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('world:LoginRequest'))

    if request.method == 'POST':
        form = PersonForm(request.POST, request.FILES)
        if form.is_valid():
            image = form.cleaned_data['image']
            name = form.cleaned_data['name']

            person = Person.objects.get(user=request.user)

            if image:
                person.image = form.cleaned_data['image']
            if name:
                person.name = form.cleaned_data['name']

            person.save() 
    return render(request,'edit.html',{'form': PersonForm()})

django forms does validation on the users data

validation is done on two levels:

  1. the field level:

    • if the field is required and the user didn't enter any data it will raise an error
    • if the user entered improperly formatted data for that field it will raise an error
    • you can override the behavior of field validation by implementing functions named as clean_fieldname
    • this type of validation results in form.field.errors
  2. the form level:

    • checks for any (non-field specific) validation errors
    • you can override the behavior by implementing clean method
    • this type of validation results in form.non_field_errors

from your models:

image is not allowed to be blank which means it is required. not entering an image will raise an error

let blank=True for both name and image

also I recommend using form.save() over saving models from the views

also there is a built in ImageField for saving images

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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