I started make a website for my school project, i use Django Frameworks
i want to make my homepage website, i already have html file, then what should i do to make Django read my homepage
enter image description here
this is my urls.py on main folder
urlpatterns = [
path('admin/', admin.site.urls),
path('katalog/', include('katalog.urls')),
path('', ) #what should i write here?
]
django doc: https://docs.djangoproject.com/en/3.0/topics/http/urls/#example, it offers the below example
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),
You have to create in your apps views.py file a FBV or CBV, the easiest is a FBV like below:
def someview(request):
context={}#if you want to add objects to the front end
return render(request, 'yourhtmlfile', context)
and then import it in your urls.py:
from . import views
urlpatterns = [
path('home/', views.someview),
Related
I would like to know if there is a way to include only specific url endpoints in my Django urls.py.
Lets say i have a app called auth with this auth/urls.py
urlpatterns = [
url(r'^password/reset/$', PasswordResetView.as_view(),
name='rest_password_reset'),
url(r'^password/reset/confirm/$', PasswordResetConfirmView.as_view(),
name='rest_password_reset_confirm'),
url(r'^login/$', LoginView.as_view(), name='rest_login'),
url(r'^logout/$', LogoutView.as_view(), name='rest_logout'),
url(r'^user/$', UserDetailsView.as_view(), name='rest_user_details'),
url(r'^password/change/$', PasswordChangeView.as_view(),
name='rest_password_change'),
]
Now I have a urls.py like that:
urlpatterns = [
path('/', include('dj_rest_auth.urls'))
]
this includes all endpoints from auth/urls.py.
Is there a way to select (in urls.py) which URL to include? Lets say I only want login and logout to be included on my urls.py.
urlpatterns = [
path('/', include('dj_rest_auth.urls.rest_login')),
path('/', include('dj_rest_auth.urls.rest_logout'))
]
Something like that, how can I make it work?
I did this long before.
# inside view.py just create your own custom view
# rest and rest_auth import
from rest_auth.views import LoginView, LogoutView
class CustomLogoutView(LogoutView):
# yes u can keep it blank.
# over-riding just to distinguish from library views.
pass
Then inside any app's urls.py import required views and and pass then into path.
this way only required url will be exposed.
from .views import CustomLoginView
urlpatterns = [
path('login/', CustomLoginView.as_view(), name='login'),
]
Also you can directly import that views (not urls) which are included in library. and then create urls (path) from them.
I have recently started using Django and I am a little lost as to why my services.html page does not render as I have followed what appears to be the same steps I took for my home page to work but I keep getting the error that nothing matches the current path. Below are my snippets for views and urls.py. I also had it be included in my website urls.py by using path('', include('affiliate.urls')),
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
[name='services']
The current path, services.html, didn't match any of these.
Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('', views.services, name="services"),
]
Views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'home.html', {})
def services(request):
return render(request, 'services.html', {})
Change your urlpatterns as follows:
urlpatterns = [
path('', views.home, name="home"),
path('services/', views.services, name="services"),
]
Now when you want to access the services view, make sure to use 127.0.0.1:8000/services
because both of your paths for home and services are pointing to the same url.
Willem is trying to explain you the same thing.
I am doing django course from udemy, i did one experiment. Below is my folder structure
Project
appTwo
urls.py
ProTwo
urls.py
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^$',views.help,name='help'),
url(r'^$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^help/',include('appTwo.urls')),
url(r'^users/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Now when i try to open the page users by http://127.0.0.1:8000/users it opens the page help.html. For http://127.0.0.1:8000/help it opens help page. When I comment the first entry in urlpatterns in urls.py it opens the users page even if i try to open help page. Can anyone please guide me what wrong I am doing or its working as expected.
you need to use different patterns for each View:
urlpatterns = [
url(r'^help$', views.help, name='help'),
url(r'^users$', views.users, name='users'),
]
the name attribute is only useful for the concept of reversing, maybe further in your course?
also, the r'' strings in python are regular expressions, you might want to learn more about them.
I got the answer for this. Below two files need to be changed
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^help$',views.help,name='help'),
url(r'^users$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^appTwo/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Explanation:
When I type in browser "basic url" i mean the address here it is http://127.0.0.1:8000/, it will go to ProTwo/urls.py which is the project folder. This will open the index page as per line url(r'^$',views.index,name='index'). If you need two configure two different page, give url(r'^appTwo/',include('appTwo.urls')), in the ProTwo/urls.py. This will call appTwo/urls.py. Now for help type http://127.0.0.1:8000/appTwo/help and for users type http://127.0.0.1:8000/appTwo/users.
I am learning django and I am trying to add a new url to '/'
here is my urls.py:
from django.contrib import admin
from django.urls import path
from blog import views as blog_views
urlpatterns = [
path(r'^$', blog_views.index),
path('admin/', admin.site.urls),
]
and here is the index method from blog/views:
def index(request):
return HttpResponse("Hey There")
But when I go to '/' route, I get a 404 response. It seems that it fails at import from blog import views as blog_views in urls.py.
What is going wrong?
Here is my project structure:
Here is the error I get:
In Django 2.x there is a path function instead of django's 1.x url function
the path function doesn't accept Regular Expressions it only accepts normal text
So to make a url for the home page with path function you only need to write your url path like this :
urlpatterns = [
path('', blog_views.index), # http://localhost/
path('admin/', admin.site.urls),
]
read more about Django 2.x urls here:
https://docs.djangoproject.com/en/dev/ref/urls/
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"),
]