user194932147
user194932147

Reputation: 963

How can i change the template for different urls

I have the search form both for public site and admin site. But they have different template but same view.

Is there any to pass the template name in urls or with some conditional logic

I have this view

class Search(View):
    form_class = SearchForm
    #i want something like that
    if url1 == /public/search
       template_name = 'search1.html'
    else
       template_name = 'search2.html'

Upvotes: 1

Views: 31

Answers (1)

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53971

If you have two separate urls, you can pass variables to your view via your urlconf;

url(r'^public/search/$', MyView.as_view(template_name="search1.html"), name= 'public_search')
url(r'^private/search/$', MyView.as_view(template_name="search2.html"), name= 'private_search')

Arguments passed into as_view() will be assigned onto the instance that is used to service a request. Using the previous example, this means that every request on MyView is able to use self.size. Arguments must correspond to attributes that already exist on the class (return True on a hasattr check).

Upvotes: 1

Related Questions