Why do my files not match a Django 1.8 installation even though Bash confirms I have one installed? - django

I set up the virtual environment variable for Django 1.8 as per the instructions in the Django tutorial (1.8) and the (almost) matching 1.7 tutorial on PythonAnywhere. When I go into Bash and follow the instructions to check the Django version it confirms that I have version 1.8. installed.
I am up to part 3 in the Django tutorial at this URL:
https://docs.djangoproject.com/en/1.8/intro/tutorial03/
In mysite/urls.py the tutorial tells me to write this:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
However, when I actually opened the file I was presented with this:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
The tutorial then has this to say:
Doesn’t match what you see? If you’re seeing admin.autodiscover()
before the definition of urlpatterns, you’re probably using a version
of Django that doesn’t match this tutorial version. You’ll want to
either switch to the older tutorial or the newer Django version.
As I said though, Bash confirms that I Do have Django 1.8 installed. What am I missing here? Why do I not have the correct files for Django 1.8 even though that's what I supposedly installed?
I tried to make the files match the tutorial files, but it only resulted in an error appearing on my public site.

The most likely cause is that you didn't activate the virtualenv when you ran startproject, so you got the default Django from PythonAnywhere instead of the one that you installed.

Related

Where does the smartselect URLS belong?

I'm trying to set op SmartSelect in my django project so I can chain dropdown menu's.
I do not understand how/where the urls belongs for the installation?
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^chaining/', include('smart_selects.urls')),
)
This is what their official installation guide says. Though this is either and old version of Django or i'm doing something wrong as I have not seen any URLS file written this way before and VScode does not recognize it.
What am I doing wrong?
As of django-3.1, url(…) [Django-doc] is
deprecated in favor of re_path(…) [Django-doc] and has been removed in django-4.0.
Furthermore a new syntax for paths has been introduced with path converters: you
use path(…) [Django-doc] for that.
So you can implement this as:
from django.urls import include, path
urlpatterns = [
path('admin/', include(admin.site.urls)),
path('chaining/', include('smart_selects.urls')),
]
That being said, if the documentation is that old, the project might no longer be "alive".

ImportError: cannot import name 'patterns'

I have a problem with Django, I created a 'login' app and added the URL on mysite/urls.py as below:
from django.conf.urls import include, patterns, url
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
url(r'^user-auth/', include('user_auth.urls')),
url(r'^file-upload/', include('file_uploader.urls')),
url(r'^pagination/', include('pagination.urls')),
patterns('login.views',
url(r'^login/', 'loginView'),
url(r'^greeting/', 'formView'),
url(r'^logout/', 'logoutView')
)
]
However, when I started the server, I received the message on console as:
File "/home/win/Python/mysite/mysite/urls.py", line 16, in <module>
from django.conf.urls import include, patterns, url
ImportError: cannot import name 'patterns'
Do you meet any problem like this? and any resolution do you have to resolve it.
Please please help me.
Thanks
In the latest release of Django (as of this post), patterns is not used.
You can use re_path for the same effect. For Example:
from django.urls import include, re_path
from django.contrib import admin
from myapp.views import *
urlpatterns = [
re_path(r'^admin', include(admin.site.urls)),
re_path(r'^$', home, name='home'),
]
For more information please follow: Documentation
FYI patterns has been removed in Django 1.10. See release 1.10 notes:
https://docs.djangoproject.com/en/2.0/releases/1.10/
If you want to use earlier versions (but i don't see why you would want to do it) , anything below that i.e. 1.9 should be ok, but do note it has been slotted for deprecation since 1.8 i think.
And if you're using django, especially if you are new, I don't see why you would want to use your own login app. Django has a very mature and customizable auth backend. For starters, I strongly suggest you check it out. Useful examples of usage at https://djangobook.com/authentication-views/
If you are using the latest version of Django, then patterns is has been deprecated. You would simply use URL and/or Path depending on if you are on 1.11 or 2.0. If you require patterns, then you would need to downgrade to an earlier Django version.

Django-CMS Will not run, 404 Page Error on initial installation

I am following the tutorial here:
http://django-cms.readthedocs.org/en/2.4.0/getting_started/tutorial.html
I have python 2.7.2 installed and this is all in a virtualenv
my pip install list:
Django (1.5.1)
django-classy-tags (0.4)
django-cms (2.4.2)
django-filer (0.9.5)
django-mptt (0.5.2)
django-polymorphic (0.5.1)
django-reversion (1.7.1)
django-sekizai (0.7)
easy-thumbnails (1.3)
html5lib (1.0b2)
MySQL-python (1.2.4)
PIL (1.1.7)
six (1.3.0)
South (0.8.1)
wsgiref (0.1.2)
Project is named dcms and this folder has the contents
dcms manage.py media static
I have also included media and static folders in dcms because I was confused in which folders that they should be placed in
When I navigate to 127.0.0.1:8000 I see
Using the URLconf defined in dcms.urls, Django tried these URL patterns, in this order:
^media/(?P<path>.*)$
^static\/(?P<path>.*)$
^en-us/
The current URL, , didn't match any of these.
My urls file is a copy/paste of what is in the tutorial I posted above
from django.conf.urls.defaults import *
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'^', 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
Initially I thought maybe it is not calling the +u urlpatterns part of the code for whatever reason, but even when I delete the if statement and just have it call urlpatterns directly in the top part I still don't get the pretty splash page that should come up
any thoughts?
I wouldn't worry about i18 at this point.
Try using the regular patterns. Add this import:
from django.conf.urls import patterns
and replace i18n_patterns with regular old patterns.
Did you create a page?
Can you get to Admin? if so go into it and create a page at /

My DJango app is responding to /static urls

Update
I figured out what was causing the stylesheets to become invisible, though I don't quite understand it all. I set DEBUG=False in settings.py to test the error handling as described in the tutorial. Somehow setting debug to false causes the static files not to be locatable. I will look further in the configs to see if I can get a clear understanding why. Until then, please feel free to answer or comment with additional info. I am still learning!
Update
I'm going through a DJango tutorial from here and I hit a roadblock. I'm up to tutorial 3 where they explain how to refactor your urls.py file when I try loading up the admin site to make sure I haven't broken it. Sure enough it looked all wierd because it was missing the stylesheets. Stylesheets are pulled from here:
http://127.0.0.1:8000/static/admin/css/base.css
When I hit that link in my browser I get the custom 404 page I configured for my app. The stylesheets were working prior but I'm not sure which change broken them. I went through my urls.py file and reverted all of the polls specific url configs to no avail. Here's my current urls.py under hellodjango (the name of my project.)
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.http import HttpResponse
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
def page_not_found(request, template_name='404.html'):
return HttpResponse("Could not find the resource you asked for...")
handler404 = 'hellodjango.urls.page_not_found'
and here's the urls.py under my polls directory:
from django.conf.urls import patterns, url
# Uncomment the next two lines to enable the admin:
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
Help?
It looks like you don't have a URL pattern for /static. As such, the static/admin/css/base.css URL doesn't match any pattern, and so you get a 404. Try something like this:
from django.conf.urls.static import static
# ...
urlpatterns = patterns('',
# ...
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
# ...
This should work for you -- go to /static/foo.css, and you should see your CSS.
It's worth noting that this is discouraged in a production environment. For your tutorial app, though, it'll work.
The staticfiles app provides a custom runserver management command that automatically serves the static files, are you sure you have the following in your settings?
INSTALLED_APPS = (
# ...
'django.contrib.staticfiles',
)
In production, you'll use the collectstatic management command that finds all of the static media and dumps it into STATIC_ROOT (this is the only purpose for this setting - it isn't used or needed during development).
Glad you figured it out. Here's why it works like this.
django.contrib.staticfiles overrides the runserver management command so that the static files are served automatically. To remind people that they shouldn't be using django to serve static files, this only happens when DEBUG = True, as you found out.
The documentation of the overridden management command explains that you can use the --insecure flag to make this work no matter the state of the DEBUG setting.

Difference between admin.site.root and admin.site.urls

In the The Django Book in chapter 6 about the Admin Site, they tell me to add the follwing URLpattern to urls.py:
urlpatterns = patterns('',
# ...
(r'^admin/', include(admin.site.urls)),
# ...
)
But to make it work on my system, I had to uncomment the following line:
(r'^admin/(.*)', admin.site.root),
Can somebody enlighten me on what the differences are?
Both Gabriel and Antti have it the wrong way round, unfortunately.
admin.site.root is the version 1.0 behaviour. If you have downloaded 1.0 or 1.0.2, that's what you should use.
However, there were some changes to the URL handling for Django's admin very recently, which are part of the yet-to-be-released 1.1. These are primarily to make it possible to use the reverse() function to look up admin URLs. So if you have a recent checkout of the code, you'll need to use admin.site.urls.
Your link is to the second edition of the Django Book, which is being updated for version 1.1 - and the docs which Gabriel refers to are also for the current checkout, which has the new version.
(Just for completeness, I'd note that versions of Django before newforms-admin was merged, prior to 1.0, used admin.urls, not admin.site.urls or admin.site.root.)
Please notice the following; I struggled because of (.*) being in the second entry below.
Works, but is deprecated:
urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root)),
)
Incorrect, and partially works:
urlpatterns = patterns('',
(r'^admin/(.*)', include(admin.site.urls)),
)
Correct, and works well:
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
The Django Book speaks of version 0.9.6. Since then the admin has been rewritten. In Django 1.0 the whole admin is served by a single view (admin.site.root) which parses the rest of the URL internally.
Compare the admin directory of 0.96.3 with the corresponding directory from 1.0.2. There is no urls.py in the latter.
from the source code for the admin.site.root function:
root(self, request, url): Handles main URL routing for the admin app.
[...] method can be used as a
Django view function that presents a
full admin interface for the
collection of registered models.