 Connect View to URL
Connect View to URL
Let's connect your View (page) to app URL.
Open the created urls.py file inside the app folder (in our case new_app folder).
It's a new file, so it will be empty.
Create the app_name and urlpatterns variables:
from django.urls import path
app_name = 'NewApp'
urlpatterns = [
    
]
In Django, the app_name variable is used to specify the application namespace for URL names within a Django app. It is defined in the urls.py file of the app. Here's a description of the app_name variable:
- The app_namevariable is a string that represents the namespace of the Django app.
- It is used to create a unique identifier for the app's URLs to avoid naming conflicts with URLs from other apps in the project.
- By setting the app_name, you can refer to specific URL patterns using the app's namespace in the Django Tamplate Language (should be described later).
- The app_nameshould be unique and preferably descriptive, reflecting the purpose or name of the app.
We have the implemented hello_world_view in views.py file:
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def hello_world_view(request):
    return HttpResponse("<h1>Hello, world!</h1>")
Import this view to the urls.py and add path to urlpatterns:
urlpatterns = [
    path('hello', hello_world_view, name='hello-world'),
]
Now we have access to this page via URL:
http://127.0.0.1:7000/app1/hello
If you want to use the app1 as endpoint URL, you can write the empty string into urlpatterns:
urlpatterns = [
    path('', hello_world_view, name='hello-world'),
]
Now, the access to this page provided by URL:
http://127.0.0.1:7000/app1/
At this point, our View is connected, and we can safely modify it:
def hello_world_view(request):
    header = "<h1>Hello, world!</h1>"
    paragraph = "<p>Go to the next chapter!</p>"
    return HttpResponse(header + paragraph)
http://127.0.0.1:7000/app1/
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Pergunte-me perguntas sobre este assunto
Resumir este capítulo
Mostrar exemplos do mundo real
Awesome!
Completion rate improved to 3.45 Connect View to URL
Connect View to URL
Deslize para mostrar o menu
Let's connect your View (page) to app URL.
Open the created urls.py file inside the app folder (in our case new_app folder).
It's a new file, so it will be empty.
Create the app_name and urlpatterns variables:
from django.urls import path
app_name = 'NewApp'
urlpatterns = [
    
]
In Django, the app_name variable is used to specify the application namespace for URL names within a Django app. It is defined in the urls.py file of the app. Here's a description of the app_name variable:
- The app_namevariable is a string that represents the namespace of the Django app.
- It is used to create a unique identifier for the app's URLs to avoid naming conflicts with URLs from other apps in the project.
- By setting the app_name, you can refer to specific URL patterns using the app's namespace in the Django Tamplate Language (should be described later).
- The app_nameshould be unique and preferably descriptive, reflecting the purpose or name of the app.
We have the implemented hello_world_view in views.py file:
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def hello_world_view(request):
    return HttpResponse("<h1>Hello, world!</h1>")
Import this view to the urls.py and add path to urlpatterns:
urlpatterns = [
    path('hello', hello_world_view, name='hello-world'),
]
Now we have access to this page via URL:
http://127.0.0.1:7000/app1/hello
If you want to use the app1 as endpoint URL, you can write the empty string into urlpatterns:
urlpatterns = [
    path('', hello_world_view, name='hello-world'),
]
Now, the access to this page provided by URL:
http://127.0.0.1:7000/app1/
At this point, our View is connected, and we can safely modify it:
def hello_world_view(request):
    header = "<h1>Hello, world!</h1>"
    paragraph = "<p>Go to the next chapter!</p>"
    return HttpResponse(header + paragraph)
http://127.0.0.1:7000/app1/
Obrigado pelo seu feedback!