Unresolved url reference(python django) - python

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',
)

Related

Show the main page in the django multi-tenant

I am studing the djanto multi-tenant package (https://github.com/django-tenants/django-tenants).
When i try to access the main page (landing page) at http://127.0.0.1:8000/, I got the following message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Raised by: core.views.home
No tenant for hostname "127.0.0.1"
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.
How do I get the landing page to show correctly?
URL FILE
from core.views import home
urlpatterns = [
path('admin/', admin.site.urls),
path('', home, name='home'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
THE VIEW OF LANDINGPAGE
from django.shortcuts import render
def home(request):
return render(request, 'core/index.html')
PARTIAL SETTINGS FILES
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
SHARED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_tenants',
'customer',
'core', //landing page
]
TENANT_APPS = [
# The following Django contrib apps must be in TENANT_APPS
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
# your tenant-specific apps
'myclientapp',
]
INSTALLED_APPS = list(set(SHARED_APPS + TENANT_APPS))
Thank you very much!
The issue there is with the Django Tenants middleware. By default, django_tenants will show a Page 404 error if you go to a tenant that does not exist. You can change this behavior by over-modifying the middleware and having your own local copy or (the easiest solution) you can just provide a default case by adding SHOW_PUBLIC_IF_NO_TENANT_FOUND = True in your project's settings.py file. This will route to the public schema hence other url routes will work as they should.

Importing Errors

I am getting errors in almost all imports please help.
i'm using django 2.2.5, python 3.7, but getting errors in unresolved import'django.contrib' ,unresolved import'django.urls',unresolved import'django.shortcuts' etc
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path("", include("hello.urls")),
path('admin/', admin.site.urls),
]
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!")
Message=No module named 'django'
File "C:\Users\Sharf\Desktop\python projects\p1\calc\urls.py", line 1,
in <module>
from django.urls import path
Can you post your settings.py file ? I think the problem comes from here ...
INSTALLED_APPS = [
'mainApp.apps.nameApp',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'channels'
]
ROOT_URLCONF = 'app.urls'

Page not found (404) in Django view

I am trying to wire up a view based on the Django tutorial (1.8), and am for some reason not getting a basic url to work:
Page not found (404)
Settings
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'events',
)
In the main folder, I have these_events/these_events/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^/', include('events.urls')),
]
In the events app, I have these_events/events/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r"^$", views.search_db, name='search-db')
]
these_events/events/views.py:
from django.shortcuts import render
from django.http import HttpResponse
def search_db(request):
return HttpResponse("hello, world")
This has me befuddled as I followed the example, and this is the way I remember using Django in the past.
In these_events/these_events/urls.py
try changing
url(r'^/', include('events.urls')),
to
url(r'', include('events.urls')),

django template page output No module named blog

Im starting with django but this get me one error and i cant run /blog/templates/index.html
ps:
i tried with
url(r'^$', 'FirstBlog.blog.views.home', name='home')
or
url(r'^$', include('views.home'))
but this dont work
i also tried
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
but....
can u help me?
full project: http://www.megafileupload.com/Uih/FirstBlog.tar.gz
django version: 1.7.7
First make sure you set TEMPLATES_DIRS in settings.py, it needs to be an absolute path.
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )
Next if your templates folder is within your app, all you need is the string 'index.html',
# Function Based View
from django.shortcuts import render
def home(request):
return render(request, 'index.html') # Render syntax requires (request, template)
# Class based view
from django.views import generic
class Home(generic.TemplateView):
template_name = 'index.html'
For class based views urls:
from blog.views import Home
urlpatterns = [
url(r'^$', Home.as_view(), name='home'),
]
For functions based views urls:
url(r'^$', 'blog.views.home', name='home')

Django "ImportError at /"

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.

Categories

Resources