No module named urls.py - django

I've been following the django tutorial over at https://docs.djangoproject.com/en/dev/intro/tutorial03/ but I've hit a brick wall.
yes I have searched google and looked up the 15 or so exact questions and nothing is working.
My directory structure is:
djangotest/
urls.py
settings.py
My settings has this
ROOT_URLCONF = 'urls.py'
The generator generated urls.py, not djangotest.urls as detailed in the tutorial. However, I don't think the naming matters too much. I've tried every variation of this urls file and I am having errors spat at me every time. The main one being the above.
urls.py
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'^reviews/$', 'djangotest.review.urls')
)
I'd love some help with this like how I can debug?

Are you being careful about the projectname and appname? Are you following the right tutorial for the version of django you downloaded? I'd also recommend using the latest stable release http://www.djangoproject.com/download/1.3.1/tarball/ rather than the latest dev release.
I'd recommend starting over; because without posting all your files (or steps you did), we aren't going to be able to debug where you went wrong.
Also, it seems like just running:
django-admin.py startproject my_test_project
cd my_test_project
django-admin.py startapp my_test_app
that you should have a directory structure like:
my_test_project/
|-- __init__.py
|-- manage.py
|-- my_test_app
| |-- __init__.py
| |-- models.py
| |-- tests.py
| `-- views.py
|-- settings.py
`-- urls.py
settings.py should have ROOT_URLCONF = 'my_test_project.urls', which reads the data in urls.py with no errors if you try
./manage runserver
and point your web browser to http://127.0.0.1:8000
On your update, it seems your specific mistake seems to be that your urls.py references 'djangotest.review.urls' Is there an app (in the installed apps) named review in the project djangotest with a urls.py? Are all the permissions correct?

Well, you need to have a file named "__init__.py" in each directory where you have .py files. These indicate to python that the directory is a python package. Also, verify the ownership and permissions on the files/folder to make sure you can access them as the user running the server.

Related

How to create hierarchical urls within django app?

I have multiple app based django project and within some apps url scheme getting complicated due to number of models. Hence I'm looking a way to make a hierarchical url structure within the app.
In my project's urls file I do the following.
from order import urls as order_urls
In the order app I have urls.py and urls directory which contains separate url patterns for each model as follows.
In the app's urls.py file I import the model's urls as follows.
from urls import rental as rental_urls
urlpatterns = [
url(r'^rental-request/', include(rental_urls)),
]
This gives me the error: ModuleNotFoundError: No module named 'urls'
If I put __init__.py it gives me circular import error.
I'm not sure this is the correct way/possible for my requirement. Anyone could explain the correct way to achieve it?
Having a folder called urls (with an __init__.py file) as well as a file urls.py in the same folder will probably cause trouble to load the module order.urls from anywhere in your project. How does Python will know which file must be loaded ?
Consider this structure:
├── main.py
├── urls
│   └── __init__.py
└── urls.py
And this content for each file:
# urls/__init__.py
urlpatterns = "I'm in folder"
# urls.py
urlpatterns = "I'm in file"
# main.py
import urls
print(urls.urlpatterns)
When you run main.py, the result is:
% python main.py
I'm in folder
Possible solutions :
You may delete the urls.py and move its content to urls/__init__.py, or rename the folder urls to avoid conflicts and updates imports accordingly (in urls.py)

How do I customize the admin page in Django 1.7?

I've been learning from the Django Documentation and have stumbled upon a road block in the section: https://docs.djangoproject.com/en/1.7/intro/tutorial02/#customize-the-admin-look-and-feel
My files are arranged as:
mysite/
mysite/
__pycache__/
...
__init__.py
settings.py
urls.py
wsgi.py
polls/
__pycache__/
...
migrations/
...
__init__.py
admin.py
models.py
tests.py
views.py
templates/
admin/
base_site.html
db.sqlite3
manage.py
I have modified the text in the file base_site.html to "Polls App" but the admin site continues to display "Django Administration".
PS: I'm using Win8
You need to place that templates folder in the project directory, not on the same level as the project as it currently is.
So place it under mysite/templates and you should be close.
EDIT: Actually, put the templates folder under your application so that it is Polls/templates
I have it at the same place as you, right under the project root. Make sure that django.contrib.admin is defined before your polls app in INSTALLED_APPS.

django cannot import name patterns on app engine as app is working fine on heroku but not working on app engine

I am learning django and i have deployed it on heroku it is working fine here it is the link http://whispering-bayou-9769.herokuapp.com/ but on app engine it is not working giving me error
500 please try again in few time
it is the url for app engine http://djangocomment.appspot.com/
Please help me i think it may be problem of app.yml because on app engine logs there is log telling me error
'exceptions.ImportError'>: cannot import name pattern i have check url.py it is fine already working on heroku
in app.yml i have wriiten
application: djangocomment
runtime: python
version: 1
api_version: 1
handlers:
- url: .*
script: /post/urls.py
env_variables:
DJANGO_SETTINGS_MODULE: 'post.settings'
urls.p[y in post folder contain the path
file structure
app/
build/
categopry/
admin.py
templates/
models.py
urls.py
tests.py
post/
urls.py
forms.py
settings.py
wsgi.py
static/
admin.py
templates/
models.py
urls.py
tests.py
template/
registration/
userprofile/
admin.py
templates/
models.py
urls.py
tests.py
app.yml
manage.py
procfile
README.md
requiremtn.txt
That really isn't how you run a Django app on GAE. What gave you the idea to point the script to urls.py?
For a start, you need to reference Django in the libraries setting in your app.yaml. Then, you need to set the script to the WSGI object within your project's wsgi.py file: perhaps something like post.wsgi.app. See the documentation.

ImportError After Moving App to Nested Folder

My application was working fine when I wanted to see whether I could organize my project in a better way. I read through this tutorial on structuring a django project.
Before my project structure was as follows:
camucamu
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
What I wanted to do was move the books app into an apps folder. Thus I did that and changed the project structure to the following:
camucamu
apps
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
I then changed the imports in views.py and admin.py
from books.models to apps.books.models.
I also changed INSTALLED_APPS in settings.py from books to apps.books.
When I then tried to run syncdb, I get the following error:
raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
django.core.exceptions.ImproperlyConfigured: ImportError apps.books: No module named apps.books
What am I messing up here so it can't find my app anymore?
Your apps folder does not have an __init__.py file so it cannot be recognized as a python module
I got the same error, following the same guide, as the last point of the following list was not cited. Make sure you performed the following changes:
Create a blank __init__.py file inside the apps folder (needed for python to recognize it as a package)
Update the import statements wherever you refer to an external app:
from projectname.apps.appname.models import YourModel, YourOtherModel
Inside settings.py edit INSTALLED_APPS such that it looks like this:
INSTALLED_APPS = (
...
# apps
'projectname.apps.appname1',
'projectname.apps.appname2',
)
This one is not specified in the guide: In all your urls.py files, update the urlpatterns!
BEFORE:
# client views
urlpatterns += patterns('appname',
...
)
AFTER:
# client views
urlpatterns += patterns('projectname.apps.appname',
...
)
Finally remember to update your changes by calling python manage.py syncdb
Hope that helped.

djangorecipe test command

I've got a question about testing my Django applications in a built out Django project.
First, I've got the same project not built out and everything works fine. This project follows the standard Django project architecture apart from putting my tests in their own directory:
django_project/
manage.py
settings.py
urls.py
app1/
models.py
views.py
urls.py
tests/
app2/
...
If I run the tests in this situation great!
This is all a little bit different when it comes to buildout environment. There I've tried to svn check my project and applications and make the paths to all of them available in my bin directory (and I hope that also means making it available for whatever magic djangorecipe is doing). Anyway this is what my buildout looks like:
[buildout]
parts =
django_project
app1
app2
django
extra-paths =
${buildout:directory}
${buildout:directory}/parts
${buildout:directory}/parts/django_project
${buildout:directory}/parts/app1
${buildout:directory}/parts/app2
[django_project]
recipe = infrae.subversion
urls =
https://svn/django_projects/trunk/ .
[app1]
recipe = infrae.subversion
urls
= https://svn/path/app1/trunk/ .
[app2]
recipe = infrae.subversion
urls =
https://svn/path/app2/trunk/ .
[django]
recipe = djangorecipe
version = 1.2
project = django_project
projectegg = django_project
wsgi=true
settings=settings
extra-paths = ${buildout:extra-paths}
test =
app1
app2
When I run the buildout I get the following directory tree.
django_buidout/
...
bin/
django
django.wsgi
test
parts/
django/
django_project/
__init__.py
settings.py
...
app1/
setup.py
app1/
__init__.py
tests/
app2/
setup.py
app2/
__init__.py
tests/
I can get to the django shell so that works. But if I run ./bin/test I get an 'ImportError: No module named django.project.urls'. (the dot notation is not a mistake my django_project.settings.URL_CONF=django_project.urls) This is interesting because if I start the shell I can import django_project.urls. In addition to that if I run the tests through the ./bin/django test app1 all the test run. app1 is interesting because it has no tests on views.
In the views tests I'm using urlresolvers.reverse and that also shows up in the traceback along with the django.tests Client() class.
Is there something in my architecture that is messed up, or is urlresolvers.reverse doing something I'm not aware of?
Many thanks,
Todd