django - 无法计算视图以分配新项目

时间:2015-05-11 08:30:30

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

我的观点中有这个,并尝试将新的truck_name分配给新产品。

当用户使用truck_name为None且没有Product实例时,脚本将转到

try:
    truck_name = Product.objects.get(user=request.user)

并跳到以下:

@login_required
def profile_edit(request):
    owner = TruckOwnerStatus.objects.get(user=request.user)
    truck_form = RegisterTruckForm()
    i = owner.id
    print i

    try:
        truck_name = Product.objects.get(user=request.user)
        if request.method == 'GET':
            if truck_name is not None:
                truck_form = RegisterTruckForm(instance=truck_name)
            else:
                truck_form = RegisterTruckForm()
            context = {
                'truck_form': truck_form,
                'truck_name': truck_name,
            }
            return render(request, 'accounts/profile_edit.html', context)

        elif request.method == 'POST':
            if truck_name is not None:
                truck_form = RegisterTruckForm(request.POST, request.FILES, instance=truck_name)
            else:
                truck_form = RegisterTruckForm(request.POST, request.FILES)

                #once clicked save
            if truck_form.is_valid():
                truck_name = truck_form.save(commit=False)
                truck_name.product = Product.objects.get(user=request.user)
                truck_form.save_m2m()
                truck_name.save()
                messages.success(request, "Successfully Saved!!")
                return HttpResponseRedirect('/')
            return render_to_response('accounts/profile_edit.html', {'truck_form': truck_form}, context_instance=RequestContext(request))

    except:
        print "hii"
        if request.method == 'POST':    
            truck_form = RegisterTruckForm(request.POST, request.FILES) 
            truck_owner = request.user
            if truck_owner is not None:
                truck_form = RegisterTruckForm(request.POST, request.FILES, instance=truck_owner)
            else:
                truck_form = RegisterTruckForm(request.POST, request.FILES)

            if truck_form.is_valid():
                truck_owner = truck_form.save(commit=False)
                truck_owner.profile = Product.objects.create(user=request.user)
                truck_form.save_m2m()
                truck_owner.profile.save()
                messages.success(request, "Successfully Saved!!")
                return HttpResponseRedirect('/')
            else:
                messages.error(request, "Please recheck")

            # return render(request, context, 'accounts/profile_edit.html',)
    context = {"truck_form": truck_form}
    return render(request, 'accounts/profile_edit.html', context)

它完成了保存表单的工作,但没有将表单中的truck_name分配给请求用户。因此,没有任何内容被分配给新产品

我尝试了

truck_name = Product.objects.get(user = request.user)in except但它返回错误,因为此用户没有Product实例,因此没有truck_name。

我可以在admin中看到该条目,但由于没有truck_name,因此无法查看该条目。但是,如果我要为同一个用户运行整个profile_edit并填写表单,则会分配truck_name。

如何分配?

以下是我的产品型号。

class Product(models.Model):
    user = models.OneToOneField(User)
    owner_name = models.CharField(max_length=120)
    email = models.EmailField(max_length=120)
    contact_number = models.IntegerField(max_length=15, null=True, blank=True)
    foodtruck_name = models.CharField(max_length=120)
    logo = models.ImageField(upload_to='foodtruck/logo/', null=True, blank=True)
    slogan = models.TextField(max_length=250, null=True, blank=True)
    about_us = models.TextField(max_length=500, null=True, blank=True)
    operating_state = models.CharField(max_length=120, choices=STATE_CHOICES)
    bsb = models.IntegerField(max_length=15, null=True, blank=True)
    account_number = models.IntegerField(max_length=15, null=True, blank=True)
    availability_link = models.CharField(max_length=300, null=True, blank=True)
    slug = models.SlugField(unique=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)
    update_defaults = models.BooleanField(default=False)


    def __unicode__(self):
        return self.foodtruck_name

    class Meta:
        unique_together = ('foodtruck_name', 'slug')

    def get_price(self):
        return self.price

    def get_absolute_url(self):
        return reverse("single_product", kwargs={"slug": self.slug})

0 个答案:

没有答案