Django "ImportError at /" - python

I am getting an import error on a very basic test site I am trying. Below is the error message:
ImportError at /
No module named tickets
Request Method: GET
Request URL: http://dcdev1.dcevolve.com/
Django Version: 1.5.1
Exception Type: ImportError
Exception Value:
No module named tickets
Exception Location: /usr/lib/python2.6/site-packages/django/utils/importlib.py in import_module, line 35
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/home/django/dcdev1',
'/usr/lib64/python26.zip',
'/usr/lib64/python2.6',
'/usr/lib64/python2.6/plat-linux2',
'/usr/lib64/python2.6/lib-tk',
'/usr/lib64/python2.6/lib-old',
'/usr/lib64/python2.6/lib-dynload',
'/usr/lib64/python2.6/site-packages',
'/usr/lib/python2.6/site-packages']
Server time: Mon, 5 Aug 2013 02:58:39 -0500
My directory structure is this:
-/home/django/dcdev1
-/home/django/manage.py
-/home/django/tickets
Under dcdev1 there are __init__.py __init__.pyc settings.py settings.pyc urls.py urls.pyc wsgi.py wsgi.pyc
Under tickets there are
__init__.py __init__.pyc models.py models.pyc templates tests.py views.py views.pyc
Relevant settings.py sections:
TEMPLATE_DIRS = (
"tickets/templates",
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'tickets',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
urls.py:
from django.conf.urls import patterns, include, url
from tickets.models import ticket_info
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'dcdev1.tickets.views.home', name='home'),
# url(r'^dcdev1/', include('dcdev1.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
models.py:
from django.db import models
class ticket_info(models.Model):
from_address = models.CharField(max_length = 30)
to_address = models.CharField(max_length = 30)
subject_id = models.CharField(max_length = 100)
body_text = models.TextField()
recv_timestamp = models.DateTimeField()
views.py
from django.shortcuts import render_to_response
from tickets.models import ticket_info
def home(request):
return render_to_response('index.html')
I am working off of the guide at http://net.tutsplus.com/tutorials/python-tutorials/python-from-scratch-creating-a-dynamic-website/
There seems to be some differences in his directory structure vs mine. I'm guessing something had changed in later django versions. I have never used django, just trying it out to see if it will work for a project. Any help would be much appreciated.

In urls.py
try these
url(r'^$', 'tickets.views.home', name='home'),
instead of these
url(r'^$', 'dcdev1.tickets.views.home', name='home'),
Also you can run python manage.py validate and maybe you will see something useful.

Related

Why does django continues naming my app as the previous name?

thanks for reading.
When I run "py manage.py makemigrations" I get the following message:
"ModuleNotFoundError: No module named 'transformaTe'"
This is the apps.py code:
from django.apps import AppConfig
class TransformateConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'transformate'
The name is updated there and in my INSTALLED_APPS:
INSTALLED_APPS = [
'transformate',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Where else should I look to change the name of the app?
This is the simplified structure of the app:
\my-app
\transformate
admin.py
apps.py
models.py
urls.py
views.py
\my-app
asgi.py
settigs.py
urls.py
wsgi.py
All this happened when I rename the app because I had a problem creating a table called transformaTe_myuser so I though all could be caused by the capitalized letter use.
Is there a better way of renaming an existing app in Django? I followed this steps, except for the migration part (Because I deleted the db and the migrations folder):
https://odwyer.software/blog/how-to-rename-an-existing-django-application
Thanks for your help.
Well, it turns out that when you run makemigrations you have to check the project urls.py file, that was the only thing that was crashing my migrations.
ORIGINAL my-app/urls. py FILE:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('transformaTe.urls')),
path('admin/', admin.site.urls),
]
NEEDED FILE:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('transformate.urls')), # Here was the bug
path('admin/', admin.site.urls),
]

Django cant import my app in urls.py

I have issues importing my app in url patterns, but everything seems in place. I get errors in from fairy_app import views and an error name 'News' is not defined
directory:
fairy-
fairy-
_init_.py
settings.py
urls.py
wsgi.py
fairy_app-
_init_.py
admin.py
models.py
tests.py
views.py
db.fairy
manage.py
views.py
from django.shortcuts import render
from django.views.generic import TemplateView
# Create your views here.
class News(TemplateView):
template_name = 'news.html'
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from fairy_app.views import News
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'fairy.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^news/', News.as_view()),
)
setting.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fairy_app',
)
In your urls.py you reference the News view without importing it. You import the views file as module.
So you can either do:
views.News.as_view()
or:
from fairy_app.views import News
2nd way is shorter but gets inconvenient if you have many view classes, so I prefer the first one.
I found the same problem,but got sorted it out.
Create a new python file in app-level for urls
use include function in fairy-/urls file to include all other urls of the app and import views in the new file
for example;
#in url.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^greet/',include('greetings.url')),
]
#created a new file(url.py) in greetings app
#in greetings/url.py
from . import views
urlpatterns = [
url(r'^$',views.greet),
]
#greet is the function in my views`
I had the same problem. I worked around it by giving the full path
from fairy.fairy_app.views
alternatively
from ..fairy_app.views

Unresolved url reference(python django)

I have a an unresolved URL request and Not quite sure what's causing the issue.
I am setting a homepage on my application for context
My view in views.py:
from django.http import HttpResponse
from django.shortcuts import render
def home(request):
return render(request, "homepage template.html")
My URL in urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
url(r'^$', 'homepage.views.home'),
enter code here
The error given in browser:
No module named 'homepage'
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.7.5
Exception Type: ImportError
Exception Value:
No module named 'homepage'
Exception Location: C:\Python34\lib\importlib\__init__.py in import_module, line 109
Python Executable: C:\Python34\python.exe
Python Version: 3.4.3
Python Path:
['C:\\Users\\jodie\\Desktop\\NtokloMH',
'C:\\Windows\\SYSTEM32\\python34.zip',
'C:\\Python34\\DLLs',
'C:\\Python34\\lib',
'C:\\Python34',
'C:\\Python34\\lib\\site-packages']
Server time: Sat, 7 Mar 2015 19:10:33 +0000
I thought I defined the module through the views.py and urls.py code, but pycharm is telling me it cannot resolve the URL.
As requested:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'musicsearch',
)
If musicsearch is your only installed application that you made yourself in django, and the views.py file is in that directory, then
urlpatterns = patterns('',
# Examples:
url(r'^$', 'homepage.views.home'),
enter code here
should be
urlpatterns = patterns('',
# Examples:
url(r'^$', 'musicsearch.views.home'),
enter code here
Otherwise, if homepage is an application that does exist, you need to add it to INSTALLED_APPS as such:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'musicsearch',
'homepage',
)

Page not found (404) django

Can anyone help me with this issue. When I try to access I get the following error.
Request Method: GET
Request URL:
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^admin/
^myproject/$ [name='home']
The current URL, , didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'myproject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^booking/$', 'booking.views.home', name ='home'),
)
views.py
from django.shortcuts import render
#..
# Create your views here.
def index(request):
return render("Hello, guesthouse!!")
settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'booking',
)
ROOT_URLCONF = 'myproject.urls'
WSGI_APPLICATION = 'myproject.wsgi.application'
admin.py
from django.contrib import admin
from booking.models import Bookings
# Register your models here.
admin.site.register(Bookings)
Thank you,
Rads
You are getting 404 because 'home' doesn't exist in views.py or by mistake, you have named the view wrong.
below i have changed the name of view to 'home' that matches the view specified in your urls.py
views.py
from django.shortcuts import render
def home(request):
"""
home view
"""
return render("Hello, guesthouse!!")
You don't appear to have a home function in booking.views.
Expanded answer: Each URL spec has a couple of parts that tell Django how to route the request. In your post you have:
url(r'^booking/$', 'booking.views.home', name ='home'),
In order for the mapping to work you need to have the function:
booking.views.home
You have three options here, the first is to implement another view function, the second is that you can rename booking.views.index to booking.views.home, and the third is that you can change the url spec to the following:
url(r'^booking/$', 'booking.views.index', name ='home'),
All three of these options assume that you have a directory structure like:
|-<project_root_dir>
| |
| |- <project>
| |- booking
| | |- <other files>
| | |- views.py
Your error is in urls.py. Your last pattern is incorrect. I think it should be
url(r'^booking/$', 'booking.views.index'),

Django admin panel doesn't work

Could someone explain me please what I'm doing wrong? I'm trying to enable admin panel in Django 1.2. But the link http://mysite.com/admin raises 404 error, and the link http://mysite.com raises an error like this:
TypeError at /
list objects are unhashable
Request Method: GET
Request URL: http://mysite/index.wsgi/
Django Version: 1.2.4
Exception Type: TypeError
Exception Value:
list objects are unhashable
Exception Location: /usr/local/lib/python2.5/re.py in _compile, line 230
Python Executable: /usr/local/bin/python
Python Version: 2.5.5
Python Path: ['/usr/local.20100210/lib/python2.5/site-packages', '/usr/local/lib/python2.5/site-packages/setuptools-0.6c11-py2.5.egg', '/usr/local/lib/python2.5/site-packages/Genshi-0.6-py2.5.egg', '/usr/local/lib/python2.5/site-packages/Babel-0.9.5-py2.5.egg', '/usr/local/lib/python2.5/site-packages/Pygments-1.4-py2.5.egg', '/usr/local/lib/python2.5/site-packages/pytz-2010o-py2.5.egg', '/usr/local/lib/python2.5/site-packages/Trac-0.12.1-py2.5.egg', '/usr/local/lib/python2.5/site-packages/IniAdmin-0.2-py2.5.egg', '/usr/local/lib/python2.5/site-packages/TracAccountManager-0.2.1dev-py2.5.egg', '/usr/local/lib/python2.5/site-packages/MySQL_python-1.2.3-py2.5-freebsd-8.2-RELEASE-amd64.egg', '/usr/local/lib/python25.zip', '/usr/local/lib/python2.5', '/usr/local/lib/python2.5/plat-freebsd8', '/usr/local/lib/python2.5/lib-tk', '/usr/local/lib/python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages', '/usr/local/lib/python2.5/site-packages/PIL', '/usr/local/lib/python2.5/site-packages', '/home/casf58/www/site2/cgi-bin/']
Server time: Tue, 11 Jun 2013 20:52:41 +0400
This is the empty test project which works fine on local machine. But it doesn't work at hosting. Of course, I uncommented all the necessary lines in urls.py and setiings.py and checked it several times. If I comment them back, the Django Welcome page is displayed.
Still can't find a solution in google...
Python v.2.5. Project uses wsgi_mod.
Changes in settings.py:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
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'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
r'^admin/', include(admin.site.urls)
)
You need to enclose your url pattern in ()
(r'^admin/', include(admin.site.urls)),
Try this:
urlpatterns = patterns('',
# Example:
# (r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)

Categories

Resources