Django Tutorial - Part 1 - URL didn't match - python

This has been asked before, but the problems were usually with placement of the mysite/urls.py or missing text somewhere. I've gone over those in detail, but doesn't apply here. I'm following the django tutorial EXACTLY, which means it hasn't referenced including the polls app in the settings.py file. I can pull up the right view if I manually type in the "polls" at the end of the url, as in "http://127.0.0.1:8000/polls" but I shouldn't have to do this for it to work. I'm also assuming the tutorial isn't wrong in some way. Link to the tutorial is: https://docs.djangoproject.com/en/1.9/intro/tutorial01/
The error I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, , didn't match any of these.
My views.py file:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
My polls/urls.py file:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
My mysite/mysite/urls.py file:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
This is the tree for the mysite/mysite directory just to show that I am in the right folder (there is no separate urls.py file in the main mysite directory):
.
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-35.pyc
│   ├── settings.cpython-35.pyc
│   ├── urls.cpython-35.pyc
│   └── wsgi.cpython-35.pyc
├── settings.py
├── urls.py
└── wsgi.py
Again, best I can tell this is to the letter following the django tutorial guidance. I'd like to fix it, but more importantly understand why it isn't working.

Have you checked the INSTALLED_APPS variable in the settings file to include the 'polls' app?

Related

How do I set up my Django urlpatterns within my app (not project)

Let's say I've got the classic "School" app within my Django project. My school/models.py contains models for both student and course. All my project files live within a directory I named config.
How do I write an include statement(s) within config/urls.py that references two separate endpoints within school/urls.py? And then what do I put in schools/urls.py?
For example, if I were trying to define an endpoint just for students, in config/urls.py I would do something like this:
from django.urls import path, include
urlpatterns = [
...
path("students/", include("school.urls"), name="students"),
...
]
And then in school/urls.py I would do something like this:
from django.urls import path
from peakbagger.views import StudentCreateView, StudentDetailView, StudentListView, StudentUpdateView, StudentDeleteView
urlpatterns = [
# ...
path("", StudentListView.as_view(), name="student-list"),
path("add/", StudentCreateView.as_view(), name="student-add"),
path("<int:pk>/", StudentDetailView.as_view(), name="student-detail"),
path("<int:pk>/update/", StudentUpdateView.as_view(), name="student-update"),
path("<int:pk>/delete/", StudentDeleteView.as_view(), name="student-delete"),
]
But how do I do I add another urlpattern to config/urls.py along the lines of something like this? The include statement needs some additional info/parameters, no?
from django.urls import path, include
urlpatterns = [
...
path("students/", include("school.urls"), name="students"),
path("courses/", include("school.urls"), name="courses"),
...
]
And then what happens inside of school/urls.py?
I'm open to suggestions, and definitely am a neophyte when it comes to the Django philosophy. Do I need an additional urls.py somewhere? I'd prefer not to put everything in config/urls.py and I'd prefer not to build a separate app for both students and courses.
I would rather create two (or more) urls.py files and then point them separately.
# directory structure
school/
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
├── urls
│   ├── course.py
│   ├── __init__.py
│   └── student.py
└── views.py
# school/urls/course.py
from django.urls import path
from school.views import CourseListView
urlpatterns = [
path("", CourseListView.as_view(), name="course_list"),
# other URLs
]
# school/urls/student.py
from django.urls import path
from school.views import StudentListView
urlpatterns = [
path("", StudentListView.as_view(), name="student_list"),
# other URLs
]
# config/urls.py
from django.urls import include, path
urlpatterns = [
path("student/", include("school.urls.student")),
path("course/", include("school.urls.course")),
# other URLs
]
The best solution for you is to make separate urls directory inside your app
For example if you have school as app then
app
├── School
│ ├── views.py
│ └── models.py
| └── urls
| └── __init__.py
| └── urls.py
| └── school_urls.py
| └── course_urls.py
Now in your main project urls you can set this way
urlpatterns = [
...
path("", include("school.urls"), name="students"),
...
]
and in urls.py of your school urls folder you can do this way
urlpatterns = [
...
path("students/", include("school.urls.school_urls"), name="students"),
path("course/", include("school.urls.course_urls"), name="course"),
...
]
and you can do add course view in course url folder and another student view in student urls file

The views that render "general" templates where they should be (home.html, about.html, etc)? | Django

I have templates for example like home.html, about.html, etc. Which are "general."
Where should the views that render these views be located?
I am not convinced to place these views in the applications of my project, since each one has a very specific purpose. It occurs to me to create an application specifically for these "general" views, but what should this application be called? Is it good practice to do it?
Another solution would be to put the views in the urlconf, as follows:
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name = 'pages/home.html'), name = 'home'), # here
...
]
But is this alternative of good practice?
Is there a much better alternative?
Just put them in your global app. Let's suppose my Django project name is project_name and I have an app called tasks. Here is what I would do:
~/projects/projects_name/
project_name/ # project dir (the one which django-admin.py creates)
...
settings/ # settings for different environments, see below
__init__.py
...
views.py # Put here global views (home, etc.)
urls.py
wsgi.py
static/ # site-specific static files
templates/ # site-specific templates
project_name/
home.html
about.html
tests/ # site-specific tests (mostly in-browser ones)
...
tasks/
...
static/ # tasks app-specific static files
templates/ # tasks app-specific templates

django href url with parameter throwing error

I have the following setup for a simple href download page:
urls.py
urlpatterns = [
url(r'^kpis/$', InternalKPIView.as_view(), name='internal_kpis'),
url(r'^tenants/$', TenantListView.as_view(), name='tenant-list'),
url(r'^tenants/(?P<pk>[0-9]+)/$', TenantStatsView.as_view(), name='tenant-stats'),
url(r'^fileformaterror/$', FileFormatErrorView.as_view(), name='file-format-error'),
url(r'^fileformaterror/download/(?P<s3_key>.*)$', FileFormatErrorDownloadView.as_view(), name='file-format-error-download'),
]
template.html:
Download
views.py:
class FileFormatErrorDownloadView(View):
def get(self, request, s3_key):
pass
But when executing I get the following error:
django.urls.exceptions.NoReverseMatch: Reverse for 'file-format-error-download' not found. 'file-format-error-download' is not a valid view function or pattern name.
Tree output of the related files:
$ tree -I "*.pyc|__pycache__"
.
├── apps.py
├── __init__.py
├── migrations
│   └── __init__.py
├── templates
│   └── backoffice
│   ├── file_format_error.html
│   └── internal_kpis.html
├── urls.py
└── views.py
3 directories, 7 files
From what you've provided it seems like the urls.py you are showing belongs to one of the applications within the project. My guess is that URLs of that application are either not included properly or included with a namespace.
why not use django2.0+? then code may as below:
urls.py
path('fileformaterror/download/<s3_key>/', FileFormatErrorDownloadView.as_view(), name='file-format-error-download')
template.html
Download
views.py
from django.shortcuts import HttpResponse
class FileFormatErrorDownloadView(View):
def get(self, request, s3_key):
return HttpResponse('success')

django cms and zinnia skeleton override

I am trying to connect zinnia to django-cms 3.0
I have launched zinnia and it works just fine. Now I am trying to start changing styles. More specifically templates/zinnia/skeleton.html override.
Once I add template to override original template - url reverse starts on failing.
NoReverseMatch at /en-us/blog/
Reverse for 'entry_archive_index' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
All of the urls use namespace as in {% url 'zinnia:entry_archive_index' %} and yet reverse in shell also just fails.
What else could be done to debug it? Maybe it's because of localization?
I have urls config:
from django.conf.urls import patterns, url, include
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = i18n_patterns(
'',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('zinnia.urls', namespace='zinnia')),
url(r'^comments/', include('django.contrib.comments.urls')),
url(r'^tinymce/', include('tinymce.urls')),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns(
'',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns
Settings:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.sites',
'django.contrib.comments',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.contenttypes',
'my_main_django_cms_app',
'cms',
'mptt',
'menus',
'south',
'sekizai',
'djangocms_text_ckeditor',
'djangocms_picture',
'djangocms_inherit',
'djangocms_googlemap',
'cmsplugin_contact',
'tinymce',
'tagging',
'zinnia_threaded_comments',
'zinnia',
'cmsplugin_zinnia',
)
And my_main_django_cms_app structure is
.
├── cms_plugins.py
├── forms.py
├── __init__.py
├── manage.py
├── models.py
├── settings.py
├── static
├── templates
│   ├── base.html
│   ├── home.html
│   └── zinnia
│   └── skeleton.html
├── urls.py
├── wsgi.py
And my versions:
Django==1.6.5
Pillow==2.4.0
South==0.8.4
argparse==1.2.1
beautifulsoup4==4.3.2
cmsplugin-contact==1.0.0
cmsplugin-zinnia==0.6
django-app-namespace-template-loader==0.1
django-blog-zinnia==0.14.1
django-classy-tags==0.5.1
django-cms==3.0
django-mptt==0.6.0
django-reversion==1.8.1
django-sekizai==0.7
django-tagging==0.3.2
django-tinymce==1.5.2
django-xmlrpc==0.1.5
djangocms-admin-style==0.2.2
djangocms-googlemap==0.0.5
djangocms-inherit==0.0.1
djangocms-picture==0.0.2
djangocms-text-ckeditor==2.1.6
gevent==1.0.1
greenlet==0.4.2
gunicorn==19.0.0
my_main_django_cms_app==0.1
html5lib==1.0b3
ipdb==0.8
ipython==2.1.0
psycopg2==2.5
pyparsing==2.0.2
pytz==2014.4
six==1.7.2
wsgiref==0.1.2
zinnia-threaded-comments==0.2
I have been trying to integrate zinnia into django cms for a few days, and here's my experience, which gets me to the point where I can use my own django cms template for zinnia, but I'm still not getting the menus provided with cmsplugin_zinnia to work.
Compared to your setup, I've made the following changes:
Deleted zinnia namespace, so url(r'^blog/', include('zinnia.urls', namespace='zinnia')) becomes url(r'^blog/', include('zinnia.urls')).
Added app_name = 'zinnia' to cmsplugin_zinnia.cmsapp.ZinniaApphook
Moved cms after zinnia and before cmsplugin_zinnia from settings.py in demo_cmsplugin_zinnia
With this, I can select Zinnia Weblog as Application under Advanced Settings for a new Django CMS page, and give it a unique Application Instance Name as suggested in the Django CMS docs. The name of the page or its url/slug field don't matter.
From here I can come up with my own skeleton.html that makes no reference to zinnia whatsoever, and have zinnia.base.html extend my new skeleton template.
However, at this point the cmsplugin_zinnia docs suggest:
'Once the apphook is registered, you can remove the inclusion of zinnia.urls in urls.py and then restart the server to see it in full effect.',
but instead I get a NoReverseMatch at /name_of_my_blog_app/ exception, which only disappears if I include the zinnia.urls as above without namespace.
As a few weeks have passed since your original post, you may have resolved this by now. If not, I hope this points you into the right direction. In case you ran into the same issues (EntryMenu not loaded) at some point and were able to resolve, please let me know.
By using dev version for django-blog-zinnia, I see 'EntryMenu not loaded' no more. All menus related errors are gone now. As my understanding goes, this is due to inherent namespace issue in zinnia. Fantomas42 looks covering it in development version.
It has been tracked on https://github.com/django-blog-zinnia/cmsplugin-zinnia/issues/29

Django - No module name site.urls

As this other SO post shows, my Django 1.4 directory structure globally looks like:
wsgi/
champis/
settings.py
settings_deployment.py
urls.py
site/
static/
css/
app.css
templates/some_app/foo.html
__init__.py
urls.py
views.py
models.py
manage.py
The project is champis, the app is site. My PYTHONPATH includes the wsgi folder (well from Django standards it should be named after the project i.e. champis, but here I'm starting from an Openshift django-example Git project).
My champis.urls:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^champis/', include('site.urls')),
url(r'^admin/', include(admin.site.urls)),
)
My site.urls module then routes to specific pages, but when trying to access on local, I have the error:
http://127.0.0.1/champis => no module name site.urls
The site app is present in my INSTALLED_APPS, and my ROOT_URLCONF is champis.urls.
Do you have an idea why ? Even moving the site folder into the champis one didn't help.
I finally managed to solve this problem by:
adding an __init__.py at project level
renaming my site app into web (app name seemed to be colliding with... something that I did not find)
Here is my current directory structure now:
wsgi/
champis/
settings.py
settings_deployment.py
urls.py
web/ <= changed app name
static/
css/
app.css
templates/some_app/foo.html
__init__.py
urls.py
views.py
models.py
manage.py
__init__.py <= added

Categories

Resources