AttributeError: module 'main.views' has no attribute 'home' - python

I'm making a django project and whenever I run "python manage.py runserver". I see the above error.
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import ToDoList, Item
# Create your views here.
def index(response, id):
ls = ToDoList.objects.get(id=id)
return render(response, "main/base.html", {})
def home(response):
return render(response, "main/home.html", {})
main/url.py
from django.urls import path
from main import views
from . import views
urlpatterns = [
path("<int:id>", views.index, name="index"),
path("", views.home, name="home")
]
mysite/url.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("main.urls")),
]
Thank you for the help.

main/urls.py
from django.urls import path
from main import views
from . import views
Those last two imports both import the name views, so the first one is overwritten by the second one.

Your home function is indented wrongly, it's currently a function inside the index function, not in the global scope.
Change it to the following
views.py
def index(response, id):
ls = ToDoList.objects.get(id=id)
return render(response, "main/base.html", {})
def home(response): # Don't indent this!
return render(response, "main/home.html", {})

There must be a configuration issue but if you want to see if website is working or def Home(): is working. Change your code to
def home(request):
return HttpResponse('<h1>Blog Home</h1>')

Related

Can't import views.py into urls.py

Just started working with Django. I created an app, mapped it on INSTALLED_APPS list and wrote a simple function in views.py. I tried to import views in urls.py but I'am getting an error: Cannot find reference 'views' in 'init.py'
views.py code:
from django.shortcuts import render
def home_page(request, *args, **kwargs):
return render(request, 'home.html')
urls.py code
from django.contrib import admin
from django.urls import path
from homepage import views #<---GETTING AN ERROR RIGHT HERE
urlpatterns = [
path('admin/', admin.site.urls),
]
Try:
from .views import home_page
If this does not work, then create a class in views.py and then put a function into the class and then import it. E.g.,
views.py
class Home(View):
def home_page(request, *args, **kwargs):
return render(request, 'home.html')
models.py
from views import Home
Make sure you did all this steps:
1 - created your app using python manage.py startapp projectApp or django-admin startapp projectApp
2 - added your app name projectApp to installed apps
3 - create urls.py file for your new app
In your views.py file
def home_page(request, *args, **kwargs):
return render(request, 'home.html')
In Urls.py file
from .views import home_page
app_name = 'projectAppName'
urlpatterns = [
path('admin/', admin.site.urls),
]
Add You app urls name to your main project directory/path urls.py file
urlpatterns = [
path('home/', include('projectApp.urls', namespace='projectAppName')),
]

URL pattern not being identified by Django

I followed a tutorial by a youtuber known as Mosh, I followed along with his Django tutorial but whenever I enter the URL pattern, it gives me error 404, page not found. This is a screen shot of the explorer tab.
I only edited the views.py, products.urls.py and pyshop.urls.py files.
views.py:
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse("Welcome to the new products page!")
def new(reqest):
return HttpResponse("New Products")
products.urls.py:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index),
path("new", views.new)
]
pyshop.urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("products/", include("products.urls"))
]
the way you have it should work if you have the url as
localhost:8000/products
#or
localhost:8000/products/new

Url mapping to views in django 1.11

I am writing a code to map a url '/products/new' to its view function 'new'. Instead of getting an HttpResponse from 'new' I get HttpResponse of 'index' function which is also inside views.py
My views.py code inside products.py is --
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('This is home page')
def new(request):
return HttpResponse('New Products')
My urls.py code inside products.py is --
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^', views.index),
url(r'^new', views.new)
]
My urls.py file inside the project file is --
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns=[
url(r'^admin/', admin.site.urls),
url(r'^products/', include('products.urls'))
]
Now when i access 127.0.0.1:8000/products I get desired result that is "This is the home page"
But when i access 127.0.0.1:8000/products/new or even 127.0.0.1:8000/products/new/xxx/xxx any random text after /products i get the same result that is "this is home page"
I don't know what is happening. Please help.
Add this url pattern in your project urls.py
url(r'^products/(?P<new>)/$', views.new),
#alasdair Thank you.
Adding the dollar to r'^new/$' is correct, because it stops it from matching new/something/else. But it's adding the dollar to ^$ that fixed the problem, by stopping it from matching new
So the corrected code in urls.py of products app is -
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$', views.index),
url(r'^new/$', views.new)
]

Python URLs is showing an error, "The empty path didn't match any of these"

Using the URLconf defined in todo.urls, Django tried these URL patterns, in this order:
^admin/
^todoapp/
The empty path didn't match any of these.
404 error is showing when I run my code which you can find below. This code is app url:
from django.contrib import admin
from django.conf.urls import url
from todoapp import views
urlpatterns = [
#url(r'^admin/', admin.site.urls),
url(r'^$',views.index,name='index'),
#todoapp/1
url(r'^(?P<task_id>[0-9]+)/$',views.detail, name='detail'),
]
And it is the main url.py
from django.contrib import admin
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^todoapp/',include('todoapp.urls')),
]
views.py is here:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Task
from django.template import loader
# Create your views here.
def index(request):
task_list = Task.objects.all()
template = loader.get_template('todoapp/index.html')
context = {
'task_list': task_list
}
return render(request, 'todoapp/index.html',context)
def detail(request, task_id):
task = Task.objects.get(pk=task_id)
context = {
'task': task,
}
return render(request,'todoapp/detail.html',context)
Why it is not working, I could not find a problem. How can I fix it?

Django views.py function

I am trying to run an enternal python script upon clicking a button in a django website, however I think the path of my external script which i have specified is in the wrong formatting:
from django.shortcuts import render, render_to_response
from subprocess import run,PIPE
import requests
import sys
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render_to_response('index.html')
#csrf_exempt
def external(request):
inp=request.POST.get('param')
out=run(sys.executable==['//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
print(out)
return render(requests, "index.html",{'data1':out})
I also have an error which says
TypeError at /external/
'bool' object is not iterable
when I run it on the local server.
My urls.py file:
from django.contrib import admin
from django.urls import path
from myapp import views as v
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', v.index, name="index"),
path("external/", v.external),
]
urlpatterns += staticfiles_urlpatterns()
In your view, you wrote:
out=run(sys.executable==['//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
But that makes not much sense: you are here comparing sys.executable (which is a string), with a list of strings, and therefore it will return False. So you then call run(False, shell=False, stdout=PIPE), but False is of course not a string.
You can rewrite this to:
out=run([sys.executable, '//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)

Categories

Resources