last day (15 days later) » 

9:01 PM
Hey
So sorry for being a huge dumbass about this, I am very new to django and I am trying to create something. OK, so do I put track_song = Track.objects.filter(slug=trackslug, user=user).first()
if track_song:
    # return song page for user

else:
    # try to return user
    track_user = Track.objects.filter(user=user).first()
    if track_user:
          # return user page
inside views.py?
 
djq
Yes, exactly. I wrote it in pseudocode but I can expand my answer a little more to include real code
 
Alright, hold on
It is throwing some strange indentation errors
Alright, so there was a second part of my question
Is there anyway to filter a instance based on a field in a template? In my track_details.html I have {{ object.user }} which returns the username. I don't want that, I want it to return the display_name from my model UserProfile.
btw It is saying "trackslug" not defined, what should I put here instead? It's a dynamic variable.
 
djq
ok, just refactored variables to be consistant
in your template you could do {{ object.user.display_name }} - does that solve it?
Also, where I've written return you need something like return render_to_response('mytemplate.html', context_instance={'object': object}) I'm guessing you have that, but just to make it clearer
 
Nope, that's my issue. track_details is inheriting from the model "Track" but the display_name field is stored in my model "UserProfile" so it does not know what to do when I give it that.
Also I am sure your code doesn't work because my view is a DetailView (It's a class, not a function)
 
djq
If you look at my url definition, I defined it as a regular django view - you can rework this approach to be a class-based-view
regarding, display_name, take a look at template tags. That is how you can filter within the the template. good luck!
 
9:21 PM
Okay I am trying to do this..
Now I'm getting     url(r'^/(?P<username>\d+)/(?P<slug>[-\w]+)/$', get_userpage, name='track-details'),
NameError: name 'get_userpage' is not defined
I've used your function in my view like so:
class TrackDetails(DetailView):
    model = Track

    def get_userpage(request, username, slug):
        user = User.objects.get(username=username)
        track_song = Track.objects.filter(slug=trackslug, user=user).first()
        if track_song:
            print()

        else:
            track_user = Track.objects.filter(user=user).first()
            if track_user:
                print()
print() is a placeholder.
Just to make sure it works.
 

last day (15 days later) »