How to make django to recognize two urls?











up vote
1
down vote

favorite












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!










share|improve this question


























    up vote
    1
    down vote

    favorite












    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!










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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!










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      Jan Wo

      83




      83
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          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.






          share|improve this answer





















          • Thanks for the explanation!
            – Jan Wo
            2 days ago


















          up vote
          4
          down vote













          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')),
          ]





          share|improve this answer























          • 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
            2 days ago












          • True, fixed, thanks
            – Gabriel Ben Compte
            2 days ago










          • Thank you I would like to accept both answers.
            – Jan Wo
            2 days ago











          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',
          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
          });


          }
          });














           

          draft saved


          draft discarded


















          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








          up vote
          0
          down vote



          accepted










          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.






          share|improve this answer





















          • Thanks for the explanation!
            – Jan Wo
            2 days ago















          up vote
          0
          down vote



          accepted










          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.






          share|improve this answer





















          • Thanks for the explanation!
            – Jan Wo
            2 days ago













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          PhilB

          815




          815












          • Thanks for the explanation!
            – Jan Wo
            2 days ago


















          • Thanks for the explanation!
            – Jan Wo
            2 days ago
















          Thanks for the explanation!
          – Jan Wo
          2 days ago




          Thanks for the explanation!
          – Jan Wo
          2 days ago












          up vote
          4
          down vote













          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')),
          ]





          share|improve this answer























          • 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
            2 days ago












          • True, fixed, thanks
            – Gabriel Ben Compte
            2 days ago










          • Thank you I would like to accept both answers.
            – Jan Wo
            2 days ago















          up vote
          4
          down vote













          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')),
          ]





          share|improve this answer























          • 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
            2 days ago












          • True, fixed, thanks
            – Gabriel Ben Compte
            2 days ago










          • Thank you I would like to accept both answers.
            – Jan Wo
            2 days ago













          up vote
          4
          down vote










          up vote
          4
          down vote









          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')),
          ]





          share|improve this answer














          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')),
          ]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Gabriel Ben Compte

          451114




          451114












          • 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
            2 days ago












          • True, fixed, thanks
            – Gabriel Ben Compte
            2 days ago










          • Thank you I would like to accept both answers.
            – Jan Wo
            2 days ago


















          • 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
            2 days ago












          • True, fixed, thanks
            – Gabriel Ben Compte
            2 days ago










          • Thank you I would like to accept both answers.
            – Jan Wo
            2 days ago
















          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
          2 days ago






          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
          2 days ago














          True, fixed, thanks
          – Gabriel Ben Compte
          2 days ago




          True, fixed, thanks
          – Gabriel Ben Compte
          2 days ago












          Thank you I would like to accept both answers.
          – Jan Wo
          2 days ago




          Thank you I would like to accept both answers.
          – Jan Wo
          2 days ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu