myproject (main app)
myapp (additional app)

Code: myproject/urls.py

from django.contrib import admin
from django.urls import path, include

from .views import HomeView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', HomeView, name='home'), ### will refer to main app views.py. 
These routes are commonly written in main app only
    path('hello/', include('myapp.urls')),
] ------------------------------------------ 
Code: myproject/settings.py
# Application definition

INSTALLED_APPS = [
    ...
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp', ### added to inform the django project that we have installed 
a new app named 'myapp'
]

------------------------------------------ 
Code: myproject/views.py
### refers to all the common route associated to main app (myproject)
from django.http import HttpResponse

def HomeView(request):
    return HttpResponse('Home page!')
------------------------------------------ 
Code: myapp/urls.py
### refers to all route which are associated with our new app 'myapp'. All 
routes declared here, can be accessed by adding a prefix '/hello/' ### 
because we have declared a word 'hello/' to refer 'myapp' in the 
'myproject/urls.py'
from django.urls import path
from .views import hello_delhi_capitals

urlpatterns = [
    path('', hello_delhi_capitals, name='hello_delhi_capitals'),
]
------------------------------------------ 
Code: myapp/urls.py
### refers to all the common route associated to 'myapp' app 
from django.http import HttpResponse

def hello_delhi_capitals(request):
    return HttpResponse('Hello Delhi Capitals!')
After running the project two urls will work here 
http://127.0.0.1:8000/
http://127.0.0.1:8000/hello/
On Sunday, 23 April, 2023 at 6:43:37 am UTC+5:30 ritik sahoo wrote:

> Exactly 
>
> On Tue, 18 Apr, 2023, 3:46 am Tarun Miri, <tarun...@gmail.com> wrote:
>
>> You may not write any string on the urls.py of myapp/urls.py 
>> Ex path('',hello_delhi_capitals,name="hello_delhi_capitals"),
>>
>> On Sat, 15 Apr, 2023, 7:29 am Muhammad Juwaini Abdul Rahman, <
>> juw...@gmail.com> wrote:
>>
>>> In your urls.py you use '/hello' but in your browser you type '/home'.
>>>
>>> On Sat, 15 Apr 2023 at 06:07, lalit upadhyay <upadhya...@gmail.com> 
>>> wrote:
>>>
>>>> *I have copied my code here. Plz fix this error:*
>>>>
>>>>
>>>> view.py
>>>> from django.http import HttpResponse
>>>>
>>>>
>>>> def hello_delhi_capitals(request):
>>>> return HttpResponse('Hello Delhi Capitals!')
>>>>
>>>>
>>>>
>>>> myproject/urls.py
>>>>
>>>> from django.contrib import admin
>>>> from django.urls import path, include
>>>>
>>>> urlpatterns = [
>>>> path('admin/', admin.site.urls),
>>>> path('hello/', include('myapp.urls')),
>>>> ]
>>>>
>>>>
>>>>
>>>> myapp/urls.py
>>>>
>>>>
>>>>
>>>> from django.urls import path
>>>> from .views import hello_delhi_capitals
>>>>
>>>> urlpatterns = [
>>>> path('hello/', hello_delhi_capitals, name='hello_delhi_capitals'),
>>>> ]
>>>>
>>>> -- 
>>>> You received this message because you are subscribed to the Google 
>>>> Groups "Django users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>> an email to django-users...@googlegroups.com.
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/django-users/b157258f-6697-4bd7-81c7-48e425b4a1edn%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/django-users/b157258f-6697-4bd7-81c7-48e425b4a1edn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to django-users...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/CAFKhtoTQB506MC1OwxvHhT23R5kK-0uC5cxcDm3YS3Q6SYM-Pg%40mail.gmail.com
>>>  
>>> <https://groups.google.com/d/msgid/django-users/CAFKhtoTQB506MC1OwxvHhT23R5kK-0uC5cxcDm3YS3Q6SYM-Pg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CA%2BznC8P5goz9Wzfreknw7vhpChMxjxb4P2jZOa%2BpP68%3D0r3FKA%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CA%2BznC8P5goz9Wzfreknw7vhpChMxjxb4P2jZOa%2BpP68%3D0r3FKA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2310a41f-d729-4e07-9349-76eee9e5d4cen%40googlegroups.com.

Reply via email to