Saturday, September 30, 2017

Setting up Django for the first time, urls.py and settings.py

I'm playing with Django for the first time.  I am using a guide called, "Python from Scratch - Creating a Dynamic Website"  by [b]Tuts+ Code[/b] from YouTube.  It's kinda old.  It was first posted in November 2011 and Django has evolved considerably.  I've had to try different commands, like 'syncdb' is no longer used.  Now it's 'migrate'. Yes, Tuts+ Code is over 6 years old, but it's the best tutorial in terms of explanations and teaching style. Anyways.


I'm getting an error saying there is something wrong with my settings.py:

$ python ../manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x7fe4876e76e0>
Traceback (most recent call last):
 File "/home/gnull/.local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
   fn(*args, **kwargs)
 File "/home/gnull/.local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
   self.check(display_num_errors=True)
 File "/home/gnull/.local/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
   include_deployment_checks=include_deployment_checks,
 File "/home/gnull/.local/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
   return checks.run_checks(**kwargs)
 File "/home/gnull/.local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
   new_errors = check(app_configs=app_configs)
 File "/home/gnull/.local/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
   return check_resolver(resolver)
 File "/home/gnull/.local/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
   return check_method()
 File "/home/gnull/.local/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
   for pattern in self.url_patterns:
 File "/home/gnull/.local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
   res = instance.__dict__[self.name] = self.func(instance)
 File "/home/gnull/.local/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
   patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
 File "/home/gnull/.local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
   res = instance.__dict__[self.name] = self.func(instance)
 File "/home/gnull/.local/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
   return import_module(self.urlconf_name)
 File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
   __import__(name)
 File "/home/gnull/Dropbox/TECH/python/2017/beginning-Django/G_Thomas/G_Thomas/urls.py", line 21, in <module>
   url(r'^$', 'blog.views.home', name='home')
 File "/home/gnull/.local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 85, in url
   raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().


Those last couple of lines in this error message are instructive. I'm not sure about the case of include() in urls.py but my url declaration indeed is a tuple.  Why is my shell telling me there is something wrong with my tuple when it should be a tuple?  I also swapped out the parentheses with square brackets to see if it would take a list as it says it can.  Either way, same error.


Here are the contents of my urls.py:


from django.conf.urls import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = ['',
    # url(r'^admin/', admin.site.urls),
url(r'^$', 'blog.views.home', name='home')
]


Take note of the square brackets and lack of the patterns function (as compared to the presence of the function in the code from the teacher below). Also commented out above is the default urlpattern that came with Django 1.11.  


So the urlpattern in Tuts+ Code's actually looks like this:


from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
urlpatterns = patterns ('',
url(r'^$', 'FirstBlog.views.home', name='home'),
)


Addressing a related issue with importing the name pattern in urls.py for Django, some folks over on stackoverflow describe their Djanog urls.py configuration file use a simple variable declaration for urlpatterns and don't bother with the function patterns() the way the YouTuber explains that it should look like.  Like, the YouTuber uses: 'urlpatterns = patterns( ... )' whereas the stackoverflow people set it as: 'urlpatterns = [ '', ...]'.  


I get the sense that I am slightly off in my understanding of the issue. Based on what you see in the traceback, what could be going on with  my scripts?


Is there any other information I could provide?


Thanks in advance.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/335764e9-ff16-4460-9e14-ce535b90bbfe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment