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')),
]
Related
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>')
I'm making a basic Todo app in Django.
While going to the admin page and clicking on the Todo option:
It gives me this error:
The "todo" string appears twice in the URL.
I have already done the migrations thing and I have added the todo.apps.TodoConfig in the INSTALLED_APPS.
Here is my code:
todo app urls.py
from django.urls import path
from todo import views
urlpatterns = [
path('', views.index),
path('todo/', views.index,)
]`
todo app views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello ")
todo app models.py
from django.db import models
from datetime import datetime
class Todo(models.Model):
title = models.CharField(max_length = 200)
text = models.TextField()
created_at = models.DateTimeField(default=datetime.now)
def __str__(self):
return self.title
todo app admin.py
from django.contrib import admin
from .models import Todo
admin.site.register(Todo)
The main project urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('ToDoList/', include('ToDoList.urls')),
path('Todo/', include('todo.urls')),
]
try saving the admin.py file
maybe in admin.py add this code:
from django.contrib import admin
from .models import Todo
admin.site.register(Todo)
Check It And Go!!!
I'm creating a basic app in my Django project. I mapped the views to url. While running this project it is showing
404 page not found.
in urls.py
from django.urls import path
from . import views
urlpatterns=[
path(' ',views.index,name="index"),
]
in views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("hello world");
in project/urls.py
from django.urls import include,path
from django.contrib import admin
urlpatterns = [
path("myapp/ ",include('myapp.urls')),
path('admin/', admin.site.urls),
]
I expect the output to be like hello world
Remove space character form your patters
In urls.py change
path(' ',views.index,name="index") to path('',views.index,name="index")
In project/urls.py change
path("myapp/ ",include('myapp.urls')), to path("myapp/",include('myapp.urls')),
I am trying to show a blogpost on the site.
Below is details of urls and view file details but still it showing a 404 not found page
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="ShopHome"),
path("blogpost/", views.blogpost, name="blogpost")
]
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request, 'blog/index.html')
def blogpost(request):
return render(request, 'blog/blogpost.html')
Showing:
404 not found page
you should include urls.py of your application to project urls.py
actually if your project name is "mysite" and your application name is "blogspot", you should include mysite/blogspot/urls.py on this file mysite/mysite/urls.py like this:
from django.urls import path, include
urlpatterns = [
path('', include('blogspot.urls')),
]
Actually, i forget to add the slash at the time of registering the url of this app in urls
This is the current project stucture
R6Scorextractor
R6Scoreex
migrations
templates
R6Scoreex
header.html
home.html
__Init__.py
settings.py
urls.py
views.py
models.py
apps.py
admin.py
tests.py
R6Scorextractor
__Init__.py
settings.py
urls.py
manage.py
R6Scorextractor/R6scoreex/urls.py
from django.conf.urls import url
from . import views
from django.conf.urls import include
urlpatterns = [
url(r'^$', views.index, name='index'),
]
R6Scorextractor/R6scoreex/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.shortcuts import render
from django.conf import settings
from django.core.files.storage import FileSystemStorage
# Create your views here.
from django.http import HttpResponse
import pdb;
def index(request):
return render(request, 'R6scoreex/home.html')
def simple_upload(request):
print "Entered simple_upload"
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
return render(request, 'R6scoreex/home.html', {
'uploaded_file_url': uploaded_file_url
})
return render(request, 'R6scoreex/home.html')
R6Scorextractor/R6Scorextractor/url.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('R6scoreex.urls')),
]
I just want to know how to call simple_upload inside views.py of R6scoreex module.How to write URL for it , server gives me 404 error when I went with the following
url(r'^/simple_upload/$', views.simple_upload, name='simple_upload'),
So what is the reason I am getting 404 error even after adding the above code what am I doing wrong here
You have a Regex problem most probably.
This pattern works for me: (R6Scorextractor/R6Scorextractor/url.py)
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^/', include('R6scoreex.urls')),
]
And (R6Scorextractor/R6scoreex/urls.py)
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^simple_upload/', views.simple_upload, name='simple_upload'),
]