I'm learning Python3. I installed python and django on my webserver.
But I have a problem with URLs.
Here's what I tried :
sctructure:
expnk.ru
HelloDjango (1)
HelloDjango (2)
setting.py
..........
urls.py
cat
...
any apps
run "cat" from ssh command:
python3 manage.py startapp cat
next im going to HelloDjango (2) file and edited file setting.py, for INSTALLED_APPS and added 'cat'
then I'm doing HelloDjango (2) -> urls.py. I added to this file the next strings:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^cat/', include('cat.urls')),
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls')),
url(r'^test/', include('test.urls')),
]
then I do: cat -> urls.py and add to this file:
from django.conf.urls import url, include
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
lastly, I edited file views, code from this file is below:
from django.shortcuts import render
from django.http import HttpResponse
def index(response):
return HttpResponse("Hello world!")
# Create your views here.
I created the cat application similar to the blog application, as a result: the blog is running, but cat is not.
Related
I've tried multiple methods of writing views but I don't think it is a problem here. App is installed in settings.py
It displays error every time.
project structure:
structure
views.py (app folder)
from django.http import HttpResponse
from django.shortcuts import render
def home_view(request):
return HttpResponse('Hello World')
url.py in apps folder
from django.urls import path
from . import views
urlpatterns = [
path('home_view/', views.home_view)
]
apps.py in app folder
from django.apps import AppConfig
class AppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app'
urls.py in store folder
from django.contrib import admin
from django.urls import path, include
from app import views
urlpatterns = [
path('app/home_view/', include('app.url')),
path('admin/', admin.site.urls),
]
error message:
error
As a django web development expert I saw some small corrections to be done:
In store urls.py file its app/ and app.urls
from django.contrib import admin
from django.urls import path, include
from app import views
urlpatterns = [
path('app/', include('app.urls')),
path('admin/', admin.site.urls),
]
Then change the app url.py file name to standard name urls.py.
also don't forget to add your app to installed_apps variable in settings.py file:
INSTALLED_APPS=[
'app',
'django.contin.auth',
#and other already specified apps
]
Rest all files have no bugs!!
According to this the correct path for HttpResponse is:
http://localhost:8000/app/home_view/
OR
http://127.0.0.1:8000/app/home_view/
In your urls.py file
Change
path('app/home_view/', include('app.url')),
To
path('', include('app.url')),
Then
On your browser go to:
127.0.0.1:8000/home_view/
The main thing it must be app.urls not app.url. change your file to url.py to urls.py, its recommended.
If you have defined path('app/home_view/', include('app.urls')) in urls.py of your store folder, then it goes to your urls.py which is in app.
In your app's urls.py you have written path('home_view/',views.home_view).
It means if you type 127.0.0.1:8000/app/home_view/home_view/ then it will render your HttpResponse that is Hello world.
when i tried to write the first urlpattern and the first view, i got this error, so i can be able to access to the authentification template, i have no idea what can be the source of this error
# my gp/urls.py
from django.contrib import admin
from django.conf.urls import url
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html')
when i try to run the server this is the error i get
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf
'gp.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.
this is my program tree
gp
apps
conge
organisation
personnel
stage
gp
pycache
init.py
settings.py
urls.py
views.py
wsgi.py
static
templates
gp
index.html
db.sqlte3
manage.py
# my gp/urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html', {})
what i edited is:
instead of from django.conf.urls import url i wrote from
django.urls import path
added {} in render function (its optional but just in case :) )
As stated in here: https://code.djangoproject.com/ticket/30728#no1
Maybe try to use latest Python version. Python 3.9 able to work
i make a startapp "guestbook" inside my django project.
here is the file list(guestbook)
__init__.py admin.py apps.py migrations models.py template tests.py urls.py views.py
guestbook/template
guestbook
guestbook/template/guestbook
index.html
i want to get the index render but i am facing error which is
File "C:\Users\_monster\Desktop\skill\django_frontend\backend\f_django\guestbook\urls.py", line 7, in <module>
path('', views.index, name='index')
AttributeError: module 'guestbook.views' has no attribute 'index'
here is my file setting
main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
# main url, folder url
path('admin/', admin.site.urls),
path('hello/', include('hello.urls')),
path('guestbook/', include('guestbook.urls'))
]
guestbook/urls.py
from django.urls import path
# import everything from views
from . import views
urlpatterns = [
path('', views.index, name='index')
]
guestbook/views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'guestbook/index.html')
so what is problem here ? the index.html are inside the folder
I had the same error. I Tried almost everything to solve the issue but couldnt.
I just changed the name of function index from views.py to index2(guestbook/views.py)in your case!! Then i replicated those changes to (guestbook/urls.py) as
urlpatterns = [
path('', views.index2, name='index')
]
It happened miraculously. And the console showed no errors anymore!! When i switched back again to whatever previously was, (that is to index from from index2) everything was running correctly
I've had the pleasure to work with somebody yesterday on the issue with my urls here Adding an additional template to an existing project errors out, but after trying everything suggested i'm still in the same situation.
My project is named mysite and my application is search.
It was suggested to add the following to my project urls.py
url(r'^search/', include('search.urls')),
When doing so I'm given the error ModuleNotFoundError: No module named 'search'.
My project urls.py is the following:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'),
url(r'^admin/', include(admin.site.urls)),
url(r'^search/', include('search.urls')),
]
I'm attempting to add the following to my app urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
from . import views
urlpatterns = [
url(r'^results/$', views.results, name='results'),
]
I have an empty view for results defined as
def results(request):
return render(request, 'results.html')
When I try to add the following POST to my form for the results it gives me the error in the first post. When I have the results url in my app.urls.py
<form action = "{% url 'results' %}" form method = "POST">
This is what my current application structure looks like. Please help get me on the right track. Thank you.
Your search directory is in your mysite directory (the one that includes settings.py. That means you should include mysite.search.urls (just as you use mysite.search in your import and INSTALLED_APPS).
from mysite.search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('mysite.search.urls')),
]
If your search directory was in your project directory (the one that includes manage.py, then you would remove mysite from the import, include() and INSTALLED_APPS.
from search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('search.urls')),
]
i am new to django and web frameworks. As said in the documentation of django 1.11.4, i changed polls/view.py as
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
after that i created polls/urls.py and write code in it as:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
finally changed mysite/urls.py as
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
when i run this project with: python manage.py runserver
it shows error when go to "http://localhost:8000/polls/":
Not Found: /polls/
[09/Aug/2017 12:01:36] "GET /polls/ HTTP/1.1" 404 1947
i add polls to installed apps. What else to do ??
Path error. Problem solved. code should be in "mysite/mysite/urls.py" not in "mysite/urls.py"
Probably you did'nt include the polls app properly in your project. Kindly post that line of code.