TemplateView
- In Django, TemplateView.as_view() is a class method provided by the TemplateView class, which is a class-based view that renders a template. as_view() is used to create an instance of the TemplateView and return it as a callable view function.
- To use TemplateView.as_view(), you need to import TemplateView from django.views.generic. The import statement would be:
from django.views.generic import TemplateView
- Once imported, you can use as_view() to create an instance of TemplateView with specific attributes and behaviors. Typically, you pass the necessary attributes as keyword arguments when calling as_view().
Here's an example of using TemplateView.as_view():
- urls.py
from django.views.generic import TemplateView
urlpatterns = [
path("", TemplateView.as_view(template_name='base.html')),
]
- runnign on this http://127.0.0.1:8000/
RedirectView
- In Django, the RedirectView.as_view() method is used to create a class-based view that redirects the user to a different URL. It is part of Django's generic views system.
- To use RedirectView, you need to import it from django.views.generic and then call the as_view() method. The import statement would be:
from django.views.generic import RedirectView
- Once imported, you can use as_view() to create an instance of RedirectView with specific attributes and behaviors. Typically, you pass the necessary attributes as keyword arguments when calling as_view().
from django.views.generic import RedirectView
urlpatterns = [
path("google", RedirectView.as_view(url='https://github.com/')),
path("app/", include("app.urls")),
path("app1", RedirectView.as_view(url='app/')),
]
- running on this http://127.0.0.1:8000/google
- So in this way to use django template and url in direct urls.py file
No comments:
Post a Comment