I carry out the project , which is considered in the official documentation of Django. The polls application works, the admin panel starts, and going to the main page gives the following:

Screenshot 01

File structure:

firstsite/ firstsite/ __init__.py settings.py urls.py wsgi.py polls/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py urls.py views.py static/ manage.py 

Some content ./firstsite/ settings.py :

 ... INSTALLED_APPS = [ ... 'polls', ] 

Content ./firstsite/ urls.py :

 from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^polls/', include('polls.urls')), ] 

Content ./polls/ urls.py :

 from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index, name='index'), ] 

Content ./polls/ views.py :

 from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("Hello, World!") 

Server config : Ubuntu 16.04.1, Virtualenv 15.0.1, Virtualenvwrapper 4.3.1-2, Python 3.5.2, Django 1.10.2, Postgre 9.5 + 173, uWSGI 2.0.12-5, Nginx 1.10.

As can be seen from the Python modules - all one to one with the tutorial, and does not work. I'm at a dead end, as I began to learn the framework recently and for the time being, I only understand the principles. Please help!

  • I understand that this tutorial is the main one and should not work - andreymal
  • About the principles: links are urls.py in urls.py in urlpatterns , and the main page is not there now, write down (do not forget to create a view) - andreymal will appear
  • By the way, there is a translation into Russian (there for version 1.9, but almost no difference) - andreymal
  • How is that? .. You put me in a dead end)) After all, before the addition of the polls application, the main one worked. And it was not necessary to prescribe it in the urls. Well, that is, out of the box, only the created application works the same, and nothing, except admin, was written to urls. I do not understand ... - Sergey
  • The default main page worked out of the box, which is only when nothing is written in urls :) - andreymal

2 answers 2

  from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^', include('polls.urls')), ] 

This is the most minimal working option, usually many guys for the main page create a separate application, say Frontend

 from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^polls/', include('polls.urls')), url(r'^', include('frontend.urls')), ] 

and view

 from django.shortcuts import render def main_page(request): return render('name_of_html.html', {}) 

url

 from django.urls import url from app_name import views urlpattenrs = [ url(r'^$', views.main_page, name='main_page') ] 
  • Thank! And do not tell me why in the official tutorial write non-working versions of the code? Or am I misunderstanding something? - Sergey
  • there is a working version, but in the tutorial they meant not the main page, but the page application. In your example, what would come to your application, the link would be http: // 0.0.0.0:8000/polls/ - ilgiz kasymov
  • Wow I can not stand this interjection, but in this case, it was “Wow!” I did not expect to see such responsiveness. Thanks a lot! I know about the link to the polls application, because I wrote the same, that everything opens except for the main page. Thanks again, and ahead of the second step of the tutorial)) - Sergey
  • In general, for some reason I thought that the application in the / firstsite / firstsite directory is the main page. And the link to it Django forms automatically. - Sergey
  • Either I am stupid, or the skis do not go ... Now the same picture as in the screenshot at the beginning of the post, but with the addition of the line: 3. ^ / - Sergey

The bottom line is:

  • Django site is a bunch of individual applications (apps) put together

  • applications should be as self-sufficient as possible and independent of each other; ideally, it should be possible to transfer them separately to completely different sites without changes (although it should be noted that this is not always possible)

  • There is a certain file with settings that starts downloading the entire site. It is worth noting that it does not have to be called settings.py and in general does not have to be inside the project.

  • In this file with the settings in INSTALLED_APPS this is the most handful of applications that are assembled

  • and in ROOT_URLCONF , a file (usually for чтототам/urls.py , more precisely in the format of a Python module for чтототам.urls , but also optional), linking links to the views of these grouped applications

Here is your case:

  • such a separate application is one thing - polls

  • polls so far does not depend on anyone except the actual jangi, and it can be easily transferred to another site, if it hurts

  • the settings firstsite/settings.py is firstsite/settings.py , and its use by default is written in the manage.py file in this form:

     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "firstsite.settings") 

    it can be changed to a third-party file by redefining the environment variable DJANGO_SETTINGS_MODULE (and this will have to be done once), but this is a completely different story

  • The polls application is written in INSTALLED_APPS , which starts all dark magic with models and admin panel that are automatically picked up by janga (note that the firstsite is not registered there and is not currently used as a django application)

  • ROOT_URLCONF = 'firstsite.urls' registered inside the settings, and already in the file firstsite/urls.py connection of links with polls/urls.py

And in your firstsite/urls.py prescribed to connect all the links that are registered in the module polls.urls (file polls/urls.py ) and to all of them add to the beginning polls/ . As a result, it turns out that http://127.0.0.1:8000/polls/ is, and http://127.0.0.1:8000/ has not been registered by anyone and there is none.

There are several ways to prescribe this:

  • trite to remove the prefix polls/ , then the main page of the site will be the main page of the polls application (available in the next answer, I will not repeat)

  • create a new application (for example, frontend ), and connect it to firstsite/urls.py without a prefix (also in the next answer, I will not repeat)

  • download some third-party django application and connect its main page as the main page of the site, but probably nobody needs it :)

  • put a redirect somewhere:

     from django.views.generic.base import RedirectView urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^polls/', include('polls.urls')), url(r'^$', RedirectView.as_view(pattern_name='index', permanent=False), name='site_index'), ] 

    in such a configuration, the opening http://127.0.0.1:8000/ will redirect to http://127.0.0.1:8000/polls/ .

By the way, about pattern_name='index' : this 'index' registered in the file polls/urls.py , this name uniquely identifies the link template to which it is attached, and this name will need to refer to the main polls from anywhere on the site (in particular, for this redirect in the example above). However, when there are many applications, almost all of them will have their own main pages (list of blog posts, store list, feedback form, etc., if you do it all as separate applications), and if they all register the name index then there will be a conflict. Therefore, it is better to fix the 'index' on, for example, 'polls_index' , which will unequivocally point to the main page of the polls application. Although in the framework of the tutorial it is not so important.

  • Somehow it turned out muddled than wanted - andreymal
  • Thank you, my dear man! Now I will try to make the main page a separate application, guided by your comment and the ilgiz kasymov code. - Sergey