I am new to Django and hence don't have much idea about it. I am trying to create an app having the following contents in my view.py file:
from django.shortcuts import render
from fusioncharts.models import City
def pie_chart(request):
labels = []
data = []
queryset = City.objects.order_by('-population')[:3]
for city in queryset:
labels.append(city.name)
data.append(city.population)
return render(request, 'pie_chart.html', {
'labels': labels,
'data': data,
})
The content of my urls.py file inside the app is as follows:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('pie-chart/', views.pie_chart, name='pie-chart'),
]
and the content of my urls.py file inside the main project folder is as follows:
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
]
In the settings.py file, I have added the following:
ROOT_URLCONF = 'Piechart.urls'
and under the TEMPLATES, added the following:
'DIRS': ['Piechart/fusioncharts/templates'],
Now while running my app, I am getting the following error:
django.core.exceptions.ImproperlyConfigured: The included URLconf 'Piechart.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
Can anyone please say what's going wrong?
you forgot to import include in the project urls.py, i was about to comment this but can't in my current reputation level.
from django.urls import path,include
Related
(I am a beginner in Django)
I am following along a tutorial in Django where we can dynamically pass some variable to a template file from associated function in views.py
When I am running the code for the first time, it works fine, but when I change the values within the context, the template does not update with the new values even when I refresh it
Here is my views.py code -
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
# return HttpResponse('<h1>Hey, welcome</h1>')
# we can dynamically send data to our index.html file as follows -
context={
'u_name': 'Ankit',
'u_age': 25,
'u_nationality': 'Indian',
}
return render(request,'index.html',context)
Here is my template index.html -
<h1>
How are you doing today {{u_name}} <br>You are {{u_age}} years old<br>Your nationality is {{u_nationality}}
</h1>
settings.py has been correctly set as well -
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR,'templates'],
Editing post to add urls.py as well (this is within the app)
from . import views
from django.urls import include,path
urlpatterns = [
path('',views.index,name='index')
]
And the urls.py in the main project -
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('MyApp.urls'))
]
Here is my output -
does not take the values from the context
Would be glad if someone could point out what error I am making. Thanks in advance
Python: 3.6.0 | Django: 3.1.4
By going through the documentation, I created one app, below is my app url.py (from app) file
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
My site urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('npimgapp/', include('npimgapp.urls')),
path('admin/', admin.site.urls)
]
My view.py file in app
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Welcome ...")
On executing this code I am getting below error:
django.core.exceptions.ImproperlyConfigured: The included URLconf
'npimgpro.urls' does not appear to have any patterns in it. If you see
valid patterns in the file then the issue is probably caused by a
circular i mport.
Also, below is my directory structure for ref:
Your directory structure is wonky; there shouldn't be two nested npimgpro folders.
Move the contents of that npimgpro folder up one level, so it's
npimgpro/
(etc.)
npimgapp/
(etc.)
manage.py
db.sqlite3
I'm developing my first app in Django and facing some issues. The server runs smoothly and I can operate the admin panel without any issues.
However, all of the app pages including the default homepage show a "404 not found" error.
I have created a templates directory in project root
updated this line in settings.py for templates,
'DIRS': [os.path.join(BASE_DIR,'templates')],
View for the app
from django.shortcuts import render
from .models import *
# Create your views here.
def customers(request):
customers = Customer.objects.all()
return render(request, "customers.html", {'customers': customers})
urls for the app
from django.urls import path from . import views
urlpatterns = [path('customers',views.customers, name='customers')]
urls for the project
from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static
urlpatterns = [
path('trips/',include('trips.urls')),
path('customers/',include('customers.urls')),
path('drivers/',include('drivers.urls')),
path('vehicles/',include('vehicles.urls')),
path('admin/', admin.site.urls), ]
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Help is appreciated
I can't see any problem with your case, you should be able to load http://localhost:8000/customers/customers. Maybe double customers was not you intention, then remove one of them (one in main urls.py one in app's).
Also when Django debug mode is active and you get a 404 because of URL mismatch, it shows you a list of URLs you have. To see it navigate to a non existing URL like http://localhost:8000/aaa. Then if you see customers/ there try http://localhost:8000/customers/. Go step by step to find the problem.
I have been trying to run a client-server application using Django. When I am trying to run my server in Django , it is giving me the following error.
django.core.exceptions.ImproperlyConfigured: The included URLconf '' does not appear to
have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
The project urls.py -
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('chat.views')),
]
App's views.py -
from django.shortcuts import render
from django.http import JsonResponse
def home(request):
if request.method == 'POST':
if request.is_ajax():
//code
return JsonResponse(data)
return render(request,'index.html')
Where am I going wrong?
include method takes app urls.py model not views.py. You need to create urls.py file inside your app and replace url(r'^', include('chat.views')) with url(r'^', include('chat.urls')) in projects urls file. See django docs.
Include method in url.py file is used to include url patterns that are specified in other file. and when you are doing this url(r'^', include('chat.views')), it is unable to find url patterns in your views file. Hence giving error this:
django.core.exceptions.ImproperlyConfigured: The included URLconf ''
does not appear to have any patterns in it. If you see valid patterns
in the file then the issue is probably caused by a circular import.
we generally create a urls.py file in our app folder and write all our url patterns regarding this app in this file.
create a new urls.py file in your app folder and write url patterns in that file.
and then include your app's urls.py file in main urls.py file like this:-
url(r'^', include('chat.urls')),
and your app's urls.py file should look like:
from django.conf.urls import url
urlpatterns = [
url(r'', views.home, name = "home")),
]
you can find out more about django urls from documentation:- django urls
And if you don't want to create new urls.py file in you app directory then you can just import your views in main urls.py file and write url pattern in this file. then your main urls.py file will look like this:-
from django.conf.urls import url,include
from django.contrib import admin
from chat.views import home
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', home, name = "home"),
]
I was recently building a django project. I have been looking through the files for a couple of hours now and can't find the problem that would result in this kind of error message. Below, I will show you all of the relevant files within the project.
base url.py:
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^display/', include('diplay.urls')),
]
app url.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
app views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h2>HEY!</h2>")
I'm not sure why this is not working because I found a similar format online, and it seemed that every line was similar to the other one. When I try running the server, it gives me the error statement
ImportError: No module named diplay.urls
Any ideas?
1, make sure your app name is diplay which is same as in your base urls.py, I think maybe there is typo, should change to:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^display/', include('display.urls')),
]
2, make sure the file name of urls should be urls.py instead of url.py in both base and app folder
Does app diplay exists or is it typo of display
> diplay.urls
Also does urls file in app exists