开发者

Django: If user has entered address previously

I want to present a user with a form if he has not filled it out previously on login but redirect him to the homepage if he has filled in information previously. How do i accomplish this?

Here's my view:

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)
               )

Am i on the right track here?

Thanks,

ADDITIONAL INFO:

SongProfile and Song are two different models. SongProfile model is as follows: class SongProfile(models.Model): song = models.OneToOneField(Song) so when im trying to save both in song and songprofile where songprofile.song saves the latest id/pk of the record created in song. is this wrong?


I'm assuming there is only one SongProfile object per user.

 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

To create a new SongProfile object with the Song created by the form:

 song = form.save()
 songprofile = SongProfile(user=request.user)
 songprofile.song = song
 songprofile.save()

EDIT AGAIN:

Fixed backwards stuff and added Song object.


You can, indeed, do a try/catch as Tim demonstrated and that will work. If you find that you are, in some cases, beginning to filter more fields or that you want a sensible default, you can do as the docs suggest and use the get_or_create() method, like so:

sp, created = SongProfile.objects.get_or_create(user=request.user)

From the docs:

Any keyword arguments passed to get_or_create() -- except an optional one called defaults -- will be used in a get() call. If an object is found, get_or_create() returns a tuple of that object and False. If an object is not found, get_or_create() will instantiate and save a new object, returning a tuple of the new object and True.

So the Boolean value indicates if the object needed to be created or not and you can use it to guide method logic.

This syntax, too, is a little briefer and cleaner.

As far as trying to save both Song and SongProfile, no, you're not wrong. As Tim demonstrated, form.save() creates the new song and songprofile.song = song, followed by the songprofile.save(), saves the songprofile's song reference.

There are a couple of small errors in the code example. One error is,

if songprofile = null:
    ...

which, were you to use it, as @Natim noted, should be,

if songprofile is None:
    ...

Another problem is in the line,

sp = SongProfile

which Python will, indeed, compile, but which assigns the SongProfile class object reference to the variable and not an instance of the class itself. What you want to do normally would be,

sp2 = SongProfile()

and that will create the object instance. If you do a dir(sp) and dir(sp2), you'll see the difference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜