No module named urls - python

I'm following the Django Tutorials, I'm at the end of part 3, at Decoupling the URLconfs, at http://docs.djangoproject.com/en/1.1/intro/tutorial03/#intro-tutorial03 and I'm getting a "No module named urls" error message.
When I change:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('mysite.polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
(r'^admin/', include(admin.site.urls)),
)
to:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
I changed include('mysite.polls.urls')), to include(mysite.polls.urls)),, but it still didn't work.
How to solve this problem?
UPDATE 2: at mysite/polls/urls.py is
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
UPDATE 4: the whole project is at
http://www.mediafire.com/?t1jvomjgjz1

I had a similar problem in my project root ... django complained that it couldn't find the module mysite.urls.
Turns out my ROOT_URLCONF variable in settings.py, which was set up using the default values, was set incorrect. Instead of "mysite.urls", it should have been simply "urls"
I changed it, and voila, it worked.

I can't re-produce the import error on my machine using your project files (Windows 7, Django 1.1.1, Python 2.6.4). Everything imported fine but the urls were not specified properly (like the tutorial shows). Fixing the code:
/mysite/urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
/mysite/polls/urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
Visit http://127.0.0.1:8000/polls/ - I received a TemplateDoesNotExist exception because the template file is missing.
I'm afraid my answer might be to reboot and try it again. ;)

Is there an __init__.py inside mysite/polls/ directory?

I also had a weird problem with "No module named mysite.urls".
The admin site was down and the my whole site.
The solution, after a few hours of searching the web, was on my side : Django is caching some of the settings in a file that he knows from an environment variable.
I just closed my terminal in which i was doing the runnserver thing and opened a new one.

I did exactly the same thing. Python newbie mistake by reading ahead. I created a file call "polls.url" thinking it was some sort of special django template file.
I misunderstood the text:
"Now that we've decoupled that, we need to decouple the polls.urls URLconf by removing the leading "polls/" from each line, and removing the lines registering the admin site. Your polls.urls file should now look like this:
"
It should really read:
"Now that we've decoupled that, we need to decouple the polls.urls URLconf by removing the leading "polls/" from each line, and removing the lines registering the admin site. Your polls/urls.py file should now look like this:
"

Late to the party but I fixed my problem by correcting a typo inside settings.py INSTALLED_APPS. I had 'webbapp' instead of 'webapp' so you might want to check on that as well (for people still having the problem)

Like Ryan Bagwell, problem was (at least temporarily) solved by changing ROOT_URLCONF in myproject/myproject/settings.py.
I had to add "myproject" to ROOT_URLCONF to have there this:
"%s.myproject.urls" % PROJECT_APP

Related

Django 1.7 getting ImportError, Cannot import name patterns

Right now when I attempt to do makemigrations, I get the infamous cannot import names pattern message, upon reviewing my urls.py file. I've been searching online for a potential solution given that I am working in Django 1.7, but I haven't found success. I still don't know why this is happening on my current version of django at all and any possible explanation on what could be causing it would be appreciated.
In my urls.py file I have
from django.conf.urls import patterns, include, url
from django.views.generic import RedirectView
from django.contrib import admin
admin.autodiscover()
from new_bridge import views
urlpatterns = patterns('',
url(r'^$', views.IndexView, name='index'),
url(r'^admin/import', views.myimport),
(r'^favicon\.ico$', RedirectView.as_view(url='/static/images/bridge_favicon.ico')),
url(r'^words_page_redirect/(?P<language>[a-zA-Z]+)/$', views.words_page_redirect),....
)
and the final line of the message I get on terminal is simply
File "/srv/bridge-repo/new_bridge/urls.py", line 1, in <module>
from django.conf.urls import patterns, include, url
Please let me know what other info I could provide to be of more help. Any input on what I could do to actually learn more about this issue, alongside resolve it, is appreciated.
In Django 1.7, you should be able to import patterns from django.conf.urls. Is it possible you are on a newer version of Django?
Also, the use of patterns is deprecated since Django 1.8, so you can actually just avoid using it and replace your code with
urlpatterns = [
url(r'^$', views.IndexView, name='index'),
url(r'^admin/import', views.myimport),
url(r'^favicon\.ico$', RedirectView.as_view(url='/static/images/bridge_favicon.ico')),
url(r'^words_page_redirect/(?P<language>[a-zA-Z]+)/$', views.words_page_redirect),....
]
Note: make sure that each entry is wrapped with the url function with this approach.

Django urls.py import not detecting

I have a working api, and I am writing a UI to the API, as a separate application in the same project. My project urls.py looks like this
from django.conf.urls import *
import search
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^api/search$', search.validation),
url(r'^ui', include('ui.urls')),
)
My UI app's urls.py looks like this
from django.conf.urls import *
import views
urlpatterns = patterns('',
(r'^ui/$', views.search_template),
)
However, when I am trying to access with my browser(domain.com:8000/ui), I am getting an error.
Using the URLconf defined in api.urls, Django tried these URL patterns, in this order:
^api/search$
^ui ^ui$
The current URL, ui, didn't match any of these.
But if I use the below mapping in the main project's urls.py, it works.
(r'^ui$', ui.views.user_template),
I tried clearing the urls.pyc to make sure it is not stale, but it still persists. Please let me know what am I doing wrong.
You shouldn't repeat ui regex in the app's urls.py:
urlpatterns = patterns('',
(r'^$', views.search_template),
)

Django "homepage" app urls.py issue

I am pretty new to Django and when I laid my site out I did it in the following way:
The project is a "portal" of sorts,
App 1 "home" is the app that houses the homepage, login, registration
and other "site-wide" functionality
App 2 "inventory" is a basic asset inventory
App 3 "dashboard" is a set of status dashboards based on assets in the inventory
Right now I am trying to add the login functionality and I have a main project urls.py that looks like this:
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('home.urls'), name='home'),
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
)
My plan is to use the inclusion of .../home/urls.py to manage any site-wide functionality such as logging in, registering, etc.
Right now the home index view shows just fine, but anything else in .../home/urls.py gives me a 404
File: /home/urls.py
from django.conf.urls import patterns, url
from home import views
urlpatterns = patterns('',
url(r'^test/$', views.index, name='home_test'),
url(r'^ajax_login/$', views.ajax_login, name='ajax_login'),
url(r'^$', views.index, name='home_index'),
)
At this point I suppose my question is two-fold: Am I approaching this correctly? If so, how can I get the desired functionality? If not, how should I change the way things are set up/laid out to do it the correct "best practices" way?
Thanks in advance!
EDIT
Got it working thanks to dt0xff and holdenweb, accepted holdenweb's answer since it was more accurate including the need to put the home url inclusion below the rest.
Here is my new portal/urls.py file for reference
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
url(r'^', include('home.urls'), name='home'),
)
The issue is with your first pattern, which will only match the empty URL. So any empty URL will cause the home/urls.py URLs to be analyzed, but only the empty one will match even in that.
Unfortunately if there is no common prefix then that pattern "^" will match every URL (since they all start at the beginning ...).
So you should either use a common prefix for all the pages, or put the home URL specification at the end to give the other URLs a chance to match before it is tested.
Django looks in urls like this:
Get list of root urls - portal/urls
Find first, which matches to current url
If it is include, go to included urls, cutting off matched part
Your problem is here
url(r'^$', include('home.urls'), name='home'),
I mean, here
'^$'
You want url to match "start and then end of url". It works good with root(dunno.com/), but not with others, because url will contain something more...
So, just remove $ and you are fine
url(r'^', include('home.urls'), name='home'),
you never need to add regex($) at project/urls.py

Django - How to use URLconfs with an apps folder?

I'm following the tutorial on the Django website but I'm trying to expand upon it. I like the organizational scheme of putting all of your apps in an "apps" folder. I'm trying to figure out the proper way to include urls.py in order to get everything to link together.
Here's my root urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('apps.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
Here's my urls.py at apps/polls/urls.py:
from django.conf.urls.defaults import *
urlpatterns=patterns('polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
)
What's the correct way to do this? Thanks!
The way you currently have it set up... the URLs for polls would be:
http://your.url.here/polls/polls/235/results/
This is probably not what you want. The include function in the urlpatterns in the root urls.py file specifies "polls/" as a prefix to all urlpatterns in the polls app. Therefore, in the polls/urls.py file, you shouldn't specify the "polls/" prefix again as it will cause duplicate prefixes.
How are you running your Django instances? If you have multiple vhosts configured in Apache then each Django instance in /apps has it's own urls.py.
I got it to work by doing this:
urlpatterns=patterns('polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
I guess the polls part is taken care of in the root urlconf

Does Django cache url regex patterns somehow?

I'm a Django newbie who needs help: Even though I change some urls in my urls.py I keep on getting the same error message from Django. Here is the relevant line from my settings.py:
ROOT_URLCONF = 'mydjango.urls'
Here is my urls.py:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mydjango/', include('mydjango.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
#(r'^admin/doc/', include(django.contrib.admindocs.urls)),
# (r'^polls/', include('mydjango.polls.urls')),
(r'^$', 'mydjango.polls.views.homepage'),
(r'^polls/$', 'mydjango.polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'mydjango.polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'mydjango.polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'mydjango.polls.views.vote'),
(r'^polls/randomTest1/', 'mydjango.polls.views.randomTest1'),
(r'^admin/', include(admin.site.urls)),
)
So I expect that whenever I visit http://mydjango.yafz.org/polls/randomTest1/ the mydjango.polls.views.randomTest1 function should run because in my polls/views.py I have the relevant function:
def randomTest1(request):
# mainText = request.POST['mainText']
return HttpResponse("Default random test")
However I keep on getting the following error message:
Page not found (404)
Request Method: GET
Request URL: http://mydjango.yafz.org/polls/randomTest1
Using the URLconf defined in mydjango.urls, Django tried these URL patterns, in this order:
1. ^$
2. ^polls/$
3. ^polls/(?P<poll_id>\d+)/$
4. ^polls/(?P<poll_id>\d+)/results/$
5. ^polls/(?P<poll_id>\d+)/vote/$
6. ^admin/
7. ^polls/randomTest/$
The current URL, polls/randomTest1, didn't match any of these.
I'm surprised because again and again I check urls.py and there is no
^polls/randomTest/$
in it, but there is
^polls/randomTest1/'
It seems like Django is somehow storing the previous contents of urls.py and I just don't know how to make my latest changes effective.
Any ideas? Why do I keep on seeing some old version of regexes when I try to load that page even though I changed my urls.py?
Django compiles the URL regexes when it starts up for performance reasons - restart your server and you should see the new URL working correctly.

Categories

Resources