ImportError: No module named app.views django 1.10 python - python

I just have started learning django in its 1.10 version. In a project (called refugio) I have created an app called mascota.
This is my views.py file for my app:
from __future__ import unicode_literals, absolute_import
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Index")
Also, I already have written my urls.py file for it:
from django.conf.urls import url
from apps.mascota.views import index
urlpatterns = [
url(r'^$/', index),
]
And I have modified the url.py file of my project:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('apps.mascota.urls')),
]
But when I run the server of my project, it sends me the next error message:
If it helps, My dir tree is the following:
REFUGIO
apps
mascota
views.py
urls.py
refugio
settings.py
urls.py
manage.py
I know this is a dummy question, but I don't know what is wrong, I have already checked my urls sentences, but I see everything ok.
I will appreciate your help.
Ps.: My app is located inside a folder called apps.
Regards.

Every Python package need __init__.py file (read this).
REFUGIO
apps
mascota
__init__.py
views.py
urls.py
__init__.py
refugio
__init__.py
settings.py
urls.py
manage.py

Change your file structure into
REFUGIO
mascota
views.py
urls.py
refugio
settings.py
urls.py
manage.py
and then change the line in urls.py
from apps.mascota.views import index
into
form mascota.views import index
Hope this helps.

Related

Django load wrong template

I am new to Django. I am trying to configure an app called 'faq', and an index page to display the different "apps", including the previous one.
I was following the Learn Django site guide to decide what was best for the template structure.
So, I configured myproject/settings.py to let load the template from templates folder: 'DIRS': ['templates'],.
In the app folder myproject/apps/faq I have created a templates folder.
In the app views myproject/apps/faq/views.py I defined the views to load the data from database and send it back to the template:
from django.shortcuts import render
from .models import *
def faqs_view(request):
faqs = FrequentAskedQuestion.objects.all()
context = { 'faqs': faqs }
return render(request, 'index.html', context)
def faq_view(request, id):
faq = FrequentAskedQuestion.objects.get(id=id)
print(faq)
context = { 'faq': faq }
return render(request, 'detail.html', context)
In the app folder I have created a URLconf configuration in myproject/apps/faq/urls.py:
from django.urls import path
from .views import faqs_view, faq_view
urlpatterns = [
path('', faqs_view, name='faq-list'),
path('<int:id>', faq_view, name='faq-detail'),
]
A parenthesis here, since the first attempt I have tried to create a project templates folder and use the project views.py and urls.pyand it didn't work I decided to create another app called main.
In the app folder myproject/apps/main I have created a templates folder.
In the app views myproject/apps/main/views.py I defined just a simple view to render the app list:
from django.shortcuts import render
def index_view(request):
return render(request, 'index.html')
In the app folder I have created also a urls.py and added the previous view:
from django.urls import path
from .views import index_view
urlpatterns = [
path('', index_view, name='site-index'),
]
In the project URLconf file I have added both apps:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('myproject.apps.main.urls'), name='site-index'),
path('admin/', admin.site.urls, name='admin-index'),
path('faq/', include('myproject.apps.faq.urls'), name='faq-index'),
]
After the previous setup I got working 'faq' app and it works great. But, whenever I go to http://localhost:8000/ it display the title from myproject/apps/faq/templates/index when it should display myproject/apps/main/templates/index.
Maybe it is worth to mention that both templates (main/templates/index and faq/templates/index) inherit from a base template that is created in each app template folder.
The file tree:
myproject
apps
faq
templates
base.html
index.html
urls.py
views.py
main
templates
base.html
index.html
urls.py
views.py
settings.py
urls.py
I am doing something wrong. So, any clues?
Typically you write the name of the app under the templates directory, so:
faq/
templates/
faq/
index.html
This creates a sort of "namespace", such that you can reference to the correct file with:
from django.shortcuts import render
def index_view(request):
return render(request, 'faq/index.html')
If you do not construct such directory, then Djangno will simply search for a file named index.html in all the template directories. So if there are multiple, it depends on the order in which the template directories are searched.

How to set url in Django?

I'm a newbie in web development and I'm learning to use Django. Unfortunately I have been stuck for more than 24 hours trying to figure out how to set the URL of a web page. I keep getting status 404 error displayed on the browser after running python server. I have checked python documentation and other documentations online but I still don't see anywhere I'm getting it wrong.
I have the following files in the main Django follow:
urls.py
from django.contrib import admin
from django.urls import path, include
urlspatterns [
path('qbank', include ('qbank.url')),
path ('admin/', admin.site.urls),
]
settings.py
INSTALLED APPS = [
'qbank'
.....
]
In my project folder(which I named qbank) I have the following files:
urls.py
from django.urls import path
from . import views
urlspatterns = [
path ('qbank'), views.index, name = 'index'
]
view.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse ('Hello from Qbank')
The way you wrote it now, it will require two qbanks, one for the "root" urls.py, and one for the urls.py in the qbanks, hence localhost:8000/qbankqbank. If you only want to access it with qbank, then you remove the qbank for example from the urls.py of the qbanks app. So then the "root" urls.py looks like:
# project_name/urls.py
from django.contrib import admin
from django.urls import path, include
urlspatterns [
path('qbank/', include('qbank.url')),
path ('admin/', admin.site.urls),
]
and the urls.py of your app:
# qbank/urls.py
from django.urls import path
from . import views
urlspatterns = [
path ('', views.index, name='index')
]

How to Change Django Default Page to your Design?

I uploaded my site to the live server, but still, I am seeing Django default page, I updated my urls.py file, but still, it's not working. and it's showing me this output. Please let me know where I am Mistaking.
I am trying to access this mydomain.com/dashboard
Using the URLconf defined in yoursite.urls, Django tried these URL patterns, in this order:
admin/
The current path, dashboard, didn't match any of these.
here is my urls.py file...
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^dashboard/', include('dashboard.urls')),
url(r'^accounts/', include('accounts.urls')),
url('', include('frontpanel.urls')),
path('', views.index, name='index'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And here is my views.py file...
def index(request):
category = Category.objects.all().order_by('created_at')
return render(request, "base.html", {'category':category})
What version of Django are you using?
Try changing url to re_path (remember to import it first). I think url has been deprecated.
After Analyzing the question u never mentioned ur app urls.py you only mentioned project urls.py . As u have url(r'^dashboard/', include('dashboard.urls')), in ur project urls.py, there must be a file in your app also named urls.py which handles urls having prefix /dashboard/ , by default django doesnt make that file you need to manually make it add ur function names to redirect . For example this is my main urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('display_report/', include("display_report.urls"))
]
then u have to make a file named urls.py in ur app also to handle the requests and redirect the the approaite functions , in my display_report app i made a urls.py that looks like this
from django.contrib import admin
from django.urls import path, include
from display_report import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [path('', views.index)]
And then it will redirect to function named index in ur views.py file inside ur app
from django.shortcuts import render, redirect, HttpResponse
# from .models import Employee, Tasks, Report, Fileupload, Fileuploadnext
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render(request."index.html")
Here my ur will be mydomain.com/display_report and my index.html file will be inside the template folder

How to refer to different templates in different apps in Django?

The structure of my project is this:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Home/
templates/
Home/
Home.html
Login/
templates/
Login/
Login.html
Application/
templates/
Application/
Application.html
Details.html
mysite/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('Home.urls', namespace="Home")),
url(r'^Application', include('Application.urls',
namespace="Application")),
url(r'^Login', include('Login.urls', namespace="Login")),
url(r'^User', include('User.urls', namespace="User")),
]
Home/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^', views.Home, name = 'Home'),
]
Application/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'Application', views.Application, name = 'Application'),
]
The structure for the views.py(s) is the same for each app (with changes in names, of course).
Home/views.py
from django.shortcuts import render
def Login(request):
return render(request, 'Login/Login.html')
I have two links in my Home.html. One is supposed to direct a user to Application.html and the other, to Login.html. I tried to do the followings:
templates/Home/Home.html
Apply Now!
Apply Now!
Login
None of them work. No errors, no redirections, nothing.
What is weird about this whole thing is that url changes for Login and Application look different, even though the structure of both apps are the same:
The url changes to:
http://localhost:8000/ApplicationApplication.html
vs
http://localhost:8000/Login/Login.html.
Would greatly appreciate any help!
mysite / urls.py
url(r'^Application', include('Application.urls',
namespace="app")),
Application/Application.urls
urlpatterns = [
url(r'Application', views.Application, name = 'application'),
]
home.html
Apply Now!
Please change to retry

Django "No Module Named URLs" error

There are many similar questions posted already, but I've already tried those solutions to no avail. I'm working through a basic Django tutorial, and here is my code:
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tango_with_django_project.views.home', name='home'),
# url(r'^tango_with_django_project/', include('tango_with_django_project.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)),
url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
)
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says hello world!")
From the settings.py file
ROOT_URLCONF = 'tango_with_django_project.urls'
Hope you all can help get me started
Let's say I have a Django project called FailBook, with two apps, posts and links. If I look into FailBook/urls.py, I will find something like
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^posts/', include('posts.urls')), ## Custom url include
url(r'^links/', include('links.urls')), ## Custom url include
)
So then, when you look into the directory structure, you will notice that there are extra two urls.py files
FailBook
|-- posts
|-- models.py
|-- urls.py
|-- views.py
|-- etc.
|-- links
|-- models.py
|-- urls.py
|-- views.py
|-- etc.
# urls.py file in the posts folder
from django.conf.urls import patterns, include, url
from .views import PostListView, PostDetailView
urlpatterns = patterns('',
url(r'^posts/', PostListView.as_view()),
url(r'^posts/(?P<post_id>\d+)', PostDetailView.as_view()),
)
# where both views are class based views, hence the as_view function call
I know this was already solved, but the solutions provided did not help me. When I had this error it was as simple as checking all of the directories that should have had urls.py files.What I discovered was that the urls.py had not been added to the SVN repository that our Django app was pulled from.
I recommend looking in the projectname->projectname->urls.py for all references to app specific urls, and verifying that the urls.py file exists for each of them.
I had this issue while doing a Pluralsight Django tutorial. Two things I noticed:
1) I was using Django 2.0, and the command url() from Django 1.11 has been replaced with re_path() in 2.0, obtained by importing as follows:
from django.urls import path, include, re_path
This replaces,
from django.conf.urls import url, include
see this: Django 2.0 release notes
and,
2) I accidentally called the file to be imported from the subfolder, url.py, not urls.py
e.g. rango/url.py not rango/urls.py.
This was probably the main issue, and all flowed smoothly after that fix.

Categories