0

我是这种语言的新手,并试图从我创建的 2 个模型中创建一个表单

models.py

from locpick.field import LocationField
from django.db import models
from django.db.models import permalink
from django.contrib import admin
from PIL import Image
from time import time

def get_upload_file_name(instance, filename):
     return "photo/%s_%s" % (str(time()).replace('.','_'), filename)

 class Journey(models.Model):
     name = models.CharField(max_length=250)
     location = LocationField()
     description = models.TextField()
     date = models.DateTimeField()

 class Meta:
      ordering = ['name']
 def __unicode__(self):
      return self.name

  @permalink
  def get_absolute_url(self):
       return ('item_detail', None, {'object_id': self.id})

  class Photo(models.Model):
       item = models.ForeignKey(Journey)
       upload = models.ImageField(upload_to=get_upload_file_name)

  class Meta:
       ordering = ['title']
  def __unicode__(self):
       return self.title

   @permalink
   def get_absolute_url(self):
        return ('photo_detail', None, {'object_id': self.id})

   class PhotoInline(admin.StackedInline):
        model = Photo

   class JourneyAdmin(admin.ModelAdmin):
         inlines = [PhotoInline]
         list_display = ('name', 'location','description')

   admin.site.register(Journey, JourneyAdmin)

表格.py

   from django import forms
   from models import Journey, Photo


   class JourneyForm(forms.ModelForm):

     class Meta:
         model = Journey
         fields = ('name','location','description','date')

   class PhotoForm(forms.ModelForm):

       class Meta:
            model = Photo
            exclude = ('item')

视图.py

     from django.template import loader, Context
     from django.shortcuts import render_to_response
     from django.http import HttpResponseRedirect
     from django.shortcuts import render_to_response
     from django.http import HttpResponse
     from locpick.models import Journey , Photo
     from forms import JourneyForm, PhotoForm
     from django.core.context_processors import csrf

     def journeys(request):
          return render_to_response('journeys.html',{'journey':Journey.objects.all()})

     def journey(request, locpick_id=1):
           return render_to_response('journey.html',{'journey':Journey.objects.get(id=locpick_id)})

     def photo(request):
           return render_to_response('journey.html',{'photo':Photo.objects.all(get(id=photo_id))})


     def create(request):
           if request.POST:
                 form1 = JourneyForm(request.POST )
                 form2 = PhotoForm(request.POST, request.FILES)
                 if form1.is_valid() and form2.is_valid():
                      b = form1.save()
                      c = form2.save(commit=False)
                      c.b = b
                      c.save()
                      return HttpResponseRedirect('/journey/all')
                else:
                      form1 = JourneyForm(prefix="form1")
                      form2 = PhotoForm(prefix="form2")

                     args  = {}
                     args.update(csrf(request))

                     args['form'] = form

                     return render_to_response('create_journey.html',args)

我收到创建 ddidnt 返回 HttpResponse 对象的错误

我哪里错了

谢谢你

4

2 回答 2

0

def create(request):方法中,views.py您只处理了POST请求。如果有GET要求怎么办。None根据 Django,您正在返回这是错误的。

Djangoviews.py方法应该总是返回它的一个HttpResponse(或一个子类)对象。这可能会导致您的崩溃。

所以它应该是这样的 -

def create(request):
    if request.POST:
        ...
        return render_to_response(...)
    elif request.GET:
        ...
        return render_to_response(...)
于 2013-05-25T04:22:54.520 回答
0
     def create(request):
         if request.POST:
             form1 = JourneyForm(request.POST )
             form2 = PhotoForm(request.POST, request.FILES)
             if form1.is_valid() and form2.is_valid():
                  b = form1.save()
                  c = form2.save(commit=False)
                  c.b = b
                  c.save()
                  return HttpResponseRedirect('/journey/all')
         else:
             form1 = JourneyForm(prefix="form1")
             form2 = PhotoForm(prefix="form2")
             args  = {}
             args.update(csrf(request))

             args['form'] = form

             return render_to_response('create_journey.html',args)
于 2013-05-25T04:23:33.967 回答