N
N
Nikolino2018-05-12 13:29:18
Django
Nikolino, 2018-05-12 13:29:18

Is it possible to specify template_name in urls.py?

I have two identical functions in views that output different templates. I want to use one function, but be able to output different templates.

def beats_by_user(request):
    u = User.objects.get(username=request.user)
    pk_list = u.profile.beats_order
    pk_list = ast.literal_eval(pk_list)
    objects = Post.objects.filter(user_beat_id__in=pk_list, author=u)
    objects = dict([(obj.user_beat_id, obj) for obj in objects])
    beats = [objects[user_beat_id] for user_beat_id in pk_list]
    return render(request, 'blog/beat_list.html', {'beats': beats})

def user_profile(request):
    u = User.objects.get(username=request.user)
    pk_list = u.profile.beats_order
    pk_list = ast.literal_eval(pk_list)
    objects = Post.objects.filter(user_beat_id__in=pk_list, author=u)
    objects = dict([(obj.user_beat_id, obj) for obj in objects])
    beats = [objects[user_beat_id] for user_beat_id in pk_list]
    return render(request, 'blog/user_main_profile.html', {'beats': beats})

The urls now looks like this:
url(r'^change-order/$', views.beats_by_user, name='beats'),
    url(r'^my-profile/$', views.user_profile, name='beats'),

Is it possible to specify one function in urls for different URLs, but at the same time display different patterns?
url(r'^change-order/$', views.user_profile, name='beats', template='тут имя шаблона'),
    url(r'^my-profile/$', views.user_profile, name='beats', template='тут имя другого шаблона'),

Or tell me, maybe there is another option not to repeat the same thing in views

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-05-12
@Nikolino

Yes, you can .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question