How to make django to recognize two urls?
I am totally new in Django and web programming and I do not even know how to ask this question precisely enough. Excuse me then if I am asking for something obvious.
I am trying to put in the same folder app two different urls in one urls.py file. I noticed that Django does not recognize them and always open the first one.
This is my app urls.py file:
from django.conf.urls import url
from second_app import views
urlpatterns = [
url(r'^$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
This is my prooject urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', include('second_app.urls')),
url(r'^help/', include('second_app.urls'))
]
and here is my views.py that is common for both pages:
from django.shortcuts import render
from django.http import HttpResponse
def help(request):
help_dict = {'help_insert':'HELP PAGE'}
return render(request, 'second_app/help.html', context=help_dict)
def index(request):
my_dict = {'insert_me':'INDEX'}
return render(request, 'second_app/index.html', context=my_dict)
And now, when I am trying to request http://127.0.0.1:8000/help, everything works fine I can see the "HELP PAGE" but when I reqest http://127.0.0.1:8000/index nothing changes.
How can I fix it?
Thanks in advance!
python django django-urls
add a comment |
I am totally new in Django and web programming and I do not even know how to ask this question precisely enough. Excuse me then if I am asking for something obvious.
I am trying to put in the same folder app two different urls in one urls.py file. I noticed that Django does not recognize them and always open the first one.
This is my app urls.py file:
from django.conf.urls import url
from second_app import views
urlpatterns = [
url(r'^$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
This is my prooject urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', include('second_app.urls')),
url(r'^help/', include('second_app.urls'))
]
and here is my views.py that is common for both pages:
from django.shortcuts import render
from django.http import HttpResponse
def help(request):
help_dict = {'help_insert':'HELP PAGE'}
return render(request, 'second_app/help.html', context=help_dict)
def index(request):
my_dict = {'insert_me':'INDEX'}
return render(request, 'second_app/index.html', context=my_dict)
And now, when I am trying to request http://127.0.0.1:8000/help, everything works fine I can see the "HELP PAGE" but when I reqest http://127.0.0.1:8000/index nothing changes.
How can I fix it?
Thanks in advance!
python django django-urls
add a comment |
I am totally new in Django and web programming and I do not even know how to ask this question precisely enough. Excuse me then if I am asking for something obvious.
I am trying to put in the same folder app two different urls in one urls.py file. I noticed that Django does not recognize them and always open the first one.
This is my app urls.py file:
from django.conf.urls import url
from second_app import views
urlpatterns = [
url(r'^$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
This is my prooject urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', include('second_app.urls')),
url(r'^help/', include('second_app.urls'))
]
and here is my views.py that is common for both pages:
from django.shortcuts import render
from django.http import HttpResponse
def help(request):
help_dict = {'help_insert':'HELP PAGE'}
return render(request, 'second_app/help.html', context=help_dict)
def index(request):
my_dict = {'insert_me':'INDEX'}
return render(request, 'second_app/index.html', context=my_dict)
And now, when I am trying to request http://127.0.0.1:8000/help, everything works fine I can see the "HELP PAGE" but when I reqest http://127.0.0.1:8000/index nothing changes.
How can I fix it?
Thanks in advance!
python django django-urls
I am totally new in Django and web programming and I do not even know how to ask this question precisely enough. Excuse me then if I am asking for something obvious.
I am trying to put in the same folder app two different urls in one urls.py file. I noticed that Django does not recognize them and always open the first one.
This is my app urls.py file:
from django.conf.urls import url
from second_app import views
urlpatterns = [
url(r'^$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
This is my prooject urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', include('second_app.urls')),
url(r'^help/', include('second_app.urls'))
]
and here is my views.py that is common for both pages:
from django.shortcuts import render
from django.http import HttpResponse
def help(request):
help_dict = {'help_insert':'HELP PAGE'}
return render(request, 'second_app/help.html', context=help_dict)
def index(request):
my_dict = {'insert_me':'INDEX'}
return render(request, 'second_app/index.html', context=my_dict)
And now, when I am trying to request http://127.0.0.1:8000/help, everything works fine I can see the "HELP PAGE" but when I reqest http://127.0.0.1:8000/index nothing changes.
How can I fix it?
Thanks in advance!
python django django-urls
python django django-urls
asked Nov 21 '18 at 18:08
Jan WoJan Wo
83
83
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In your app url.py
file, both rules match the same thing. Let's analyze this. First, the project wide urls.py
:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', include('second_app.urls')),
url(r'^help/', include('second_app.urls'))
]
So, regardless of whether you are going to index/
or help/
, you end up then looking at second_app.urls
. So far, so good, that may make sense...
But then:
urlpatterns = [
url(r'^$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
Regardless of how you got here (through index/ or help/), the first rule will match if you have nothing else in the URL (after all, it has no idea how you got to this point), and you will get the help view. Given this file, there is simply no way to know you meant to go to "index". Think of this file as a single entity once you get here. It doesn't know what precedes it. It just tries to match what it is given at this point.
Thanks for the explanation!
– Jan Wo
Nov 21 '18 at 18:46
add a comment |
You have a bad configuration in the urls, normally there are configured like that.
In your app urls file:
from django.conf.urls import url
from second_app import views
urlpatterns = [
url(r'^help/$', views.help, name='help'),
url(r'^index/$', views.index, name='index'),
]
In your project urls file:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('second_app.urls')),
]
No, you can't have^$
in the include, that won't work. And you should have $ for each pattern in the app urls.
– Daniel Roseman
Nov 21 '18 at 18:25
True, fixed, thanks
– Gabriel Ben Compte
Nov 21 '18 at 18:30
Thank you I would like to accept both answers.
– Jan Wo
Nov 21 '18 at 18:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418154%2fhow-to-make-django-to-recognize-two-urls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your app url.py
file, both rules match the same thing. Let's analyze this. First, the project wide urls.py
:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', include('second_app.urls')),
url(r'^help/', include('second_app.urls'))
]
So, regardless of whether you are going to index/
or help/
, you end up then looking at second_app.urls
. So far, so good, that may make sense...
But then:
urlpatterns = [
url(r'^$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
Regardless of how you got here (through index/ or help/), the first rule will match if you have nothing else in the URL (after all, it has no idea how you got to this point), and you will get the help view. Given this file, there is simply no way to know you meant to go to "index". Think of this file as a single entity once you get here. It doesn't know what precedes it. It just tries to match what it is given at this point.
Thanks for the explanation!
– Jan Wo
Nov 21 '18 at 18:46
add a comment |
In your app url.py
file, both rules match the same thing. Let's analyze this. First, the project wide urls.py
:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', include('second_app.urls')),
url(r'^help/', include('second_app.urls'))
]
So, regardless of whether you are going to index/
or help/
, you end up then looking at second_app.urls
. So far, so good, that may make sense...
But then:
urlpatterns = [
url(r'^$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
Regardless of how you got here (through index/ or help/), the first rule will match if you have nothing else in the URL (after all, it has no idea how you got to this point), and you will get the help view. Given this file, there is simply no way to know you meant to go to "index". Think of this file as a single entity once you get here. It doesn't know what precedes it. It just tries to match what it is given at this point.
Thanks for the explanation!
– Jan Wo
Nov 21 '18 at 18:46
add a comment |
In your app url.py
file, both rules match the same thing. Let's analyze this. First, the project wide urls.py
:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', include('second_app.urls')),
url(r'^help/', include('second_app.urls'))
]
So, regardless of whether you are going to index/
or help/
, you end up then looking at second_app.urls
. So far, so good, that may make sense...
But then:
urlpatterns = [
url(r'^$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
Regardless of how you got here (through index/ or help/), the first rule will match if you have nothing else in the URL (after all, it has no idea how you got to this point), and you will get the help view. Given this file, there is simply no way to know you meant to go to "index". Think of this file as a single entity once you get here. It doesn't know what precedes it. It just tries to match what it is given at this point.
In your app url.py
file, both rules match the same thing. Let's analyze this. First, the project wide urls.py
:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', include('second_app.urls')),
url(r'^help/', include('second_app.urls'))
]
So, regardless of whether you are going to index/
or help/
, you end up then looking at second_app.urls
. So far, so good, that may make sense...
But then:
urlpatterns = [
url(r'^$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
Regardless of how you got here (through index/ or help/), the first rule will match if you have nothing else in the URL (after all, it has no idea how you got to this point), and you will get the help view. Given this file, there is simply no way to know you meant to go to "index". Think of this file as a single entity once you get here. It doesn't know what precedes it. It just tries to match what it is given at this point.
answered Nov 21 '18 at 18:31
PhilBPhilB
8115
8115
Thanks for the explanation!
– Jan Wo
Nov 21 '18 at 18:46
add a comment |
Thanks for the explanation!
– Jan Wo
Nov 21 '18 at 18:46
Thanks for the explanation!
– Jan Wo
Nov 21 '18 at 18:46
Thanks for the explanation!
– Jan Wo
Nov 21 '18 at 18:46
add a comment |
You have a bad configuration in the urls, normally there are configured like that.
In your app urls file:
from django.conf.urls import url
from second_app import views
urlpatterns = [
url(r'^help/$', views.help, name='help'),
url(r'^index/$', views.index, name='index'),
]
In your project urls file:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('second_app.urls')),
]
No, you can't have^$
in the include, that won't work. And you should have $ for each pattern in the app urls.
– Daniel Roseman
Nov 21 '18 at 18:25
True, fixed, thanks
– Gabriel Ben Compte
Nov 21 '18 at 18:30
Thank you I would like to accept both answers.
– Jan Wo
Nov 21 '18 at 18:48
add a comment |
You have a bad configuration in the urls, normally there are configured like that.
In your app urls file:
from django.conf.urls import url
from second_app import views
urlpatterns = [
url(r'^help/$', views.help, name='help'),
url(r'^index/$', views.index, name='index'),
]
In your project urls file:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('second_app.urls')),
]
No, you can't have^$
in the include, that won't work. And you should have $ for each pattern in the app urls.
– Daniel Roseman
Nov 21 '18 at 18:25
True, fixed, thanks
– Gabriel Ben Compte
Nov 21 '18 at 18:30
Thank you I would like to accept both answers.
– Jan Wo
Nov 21 '18 at 18:48
add a comment |
You have a bad configuration in the urls, normally there are configured like that.
In your app urls file:
from django.conf.urls import url
from second_app import views
urlpatterns = [
url(r'^help/$', views.help, name='help'),
url(r'^index/$', views.index, name='index'),
]
In your project urls file:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('second_app.urls')),
]
You have a bad configuration in the urls, normally there are configured like that.
In your app urls file:
from django.conf.urls import url
from second_app import views
urlpatterns = [
url(r'^help/$', views.help, name='help'),
url(r'^index/$', views.index, name='index'),
]
In your project urls file:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('second_app.urls')),
]
edited Nov 21 '18 at 18:29
answered Nov 21 '18 at 18:15
Gabriel Ben CompteGabriel Ben Compte
456114
456114
No, you can't have^$
in the include, that won't work. And you should have $ for each pattern in the app urls.
– Daniel Roseman
Nov 21 '18 at 18:25
True, fixed, thanks
– Gabriel Ben Compte
Nov 21 '18 at 18:30
Thank you I would like to accept both answers.
– Jan Wo
Nov 21 '18 at 18:48
add a comment |
No, you can't have^$
in the include, that won't work. And you should have $ for each pattern in the app urls.
– Daniel Roseman
Nov 21 '18 at 18:25
True, fixed, thanks
– Gabriel Ben Compte
Nov 21 '18 at 18:30
Thank you I would like to accept both answers.
– Jan Wo
Nov 21 '18 at 18:48
No, you can't have
^$
in the include, that won't work. And you should have $ for each pattern in the app urls.– Daniel Roseman
Nov 21 '18 at 18:25
No, you can't have
^$
in the include, that won't work. And you should have $ for each pattern in the app urls.– Daniel Roseman
Nov 21 '18 at 18:25
True, fixed, thanks
– Gabriel Ben Compte
Nov 21 '18 at 18:30
True, fixed, thanks
– Gabriel Ben Compte
Nov 21 '18 at 18:30
Thank you I would like to accept both answers.
– Jan Wo
Nov 21 '18 at 18:48
Thank you I would like to accept both answers.
– Jan Wo
Nov 21 '18 at 18:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418154%2fhow-to-make-django-to-recognize-two-urls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown