Django:如果用户先前已输入地址

Eva*_*611 2 django django-templates django-forms django-views

我希望向用户提供表格,如果他之前没有在登录时填写,但如果他之前填写了信息,则将其重定向到主页.我该如何做到这一点?

这是我的观点:

def makinginfo(request):
    form = SongForm(request.POST or None)
    songprofile =  SongProfile.objects.get().filter(user=request.user)
    if songprofile = null: IS THIS RIGHT?     
        if form.is_valid():
            form.save()
            sp = SongProfile
            sp.song = form.pk
            sp.save()
            if 'next' in request.POST:
               next = request.POST['next']
            else:
               next = reverse('index_show')
               return HttpResponseRedirect(next)
        return render_to_response(
        'song/create.html',
           {'form':form},

             context_instance = RequestContext(request)
               )

     else:
        return render_to_response(
        'song/show.html',
         context_instance = RequestContext(request)
               )
Run Code Online (Sandbox Code Playgroud)

我在这里走在正确的轨道上吗?

谢谢,

附加信息:

SongProfile和Song是两种不同的模型.SongProfile模型如下: class SongProfile(models.Model): song = models.OneToOneField(Song) 所以当我试图在songprofile.song保存歌曲中创建的记录的最新id/pk时,在歌曲和歌曲中保存.这是错的吗?

Tim*_*tes 5

我假设每个用户只有一个SongProfile对象.

 try:
    songprofile = SongProfile.objects.get(user=request.user)
    # Render song/show.html
 except SongProfile.DoesNotExist:
   if form.is_valid():
      # Process post

   # Render song/create.html
Run Code Online (Sandbox Code Playgroud)

要使用表单创建的乐曲创建新的SongProfile对象:

 song = form.save()
 songprofile = SongProfile(user=request.user)
 songprofile.song = song
 songprofile.save()
Run Code Online (Sandbox Code Playgroud)

再次编辑:

修复了向后的东西并添加了Song对象.