Issue
This is the current files I am using based on the poll voting beginners tutorial
mysite/urls.py (main app with a settings.py)
from django.contrib import admin
from django.urls import include, path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path("polls/", include("polls.urls")),
path('', ?),
]
polls/urls.py - Currently contains the following information. notice even though path("", ..) looks like "domainname.com/" root location, it is "domainname.com/polls/" is that became of the app_name = "polls" namespace?) How do I add a mapping for the root of the domain (domainname.com/) so I can add my own homepage?
from django.urls import path
from . import views
app_name = "polls" # Namespace to url template tag can find correct url
urlpatterns = [
# ex: /polls/
path("", views.index, name="index"),
# ex: /polls/5/
path("<int:question_id>/", views.detail, name="detail"),
# ex: /polls/5/results/
path("<int:question_id>/results/", views.results, name="results"),
# ex: /polls/5/vote/
path("<int:question_id>/vote/", views.vote, name="vote"),
]
I have noticed some github projects are mapping the home page like so
videoapp/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("videos.urls")),
]
And mapping them to the view like so
videos/urls.py
from django.urls import path
from . import views
# http://127.0.0.1:8000/
# http://127.0.0.1:8000/home
# http://127.0.0.1:8000/videos
# http://127.0.0.1:8000/videos/1
# http://127.0.0.1:8000/videos/video-1
urlpatterns =[
path("",views.home,name="home"),
path("home",views.home),
path("videos",views.videos,name="videos"),
path("videos/<int:id>",views.videodetails,name="details"),
]
However I cannot do this as it automatically is starting the url from 'polls/'. What is a good approach for me to add a custom homepage?
Edit: I've noticed many people are creating a separate app call 'home' or 'frontpage' for the soul purpose of having a homepage. It seems a little unusual to have an app just for 1 page or is this the norm?
Edit 2: The app I choose to have my homepage within cannot have any namespace applied Im guessing (cannot set app_name = "value")?
Solution
to add custom homepage to your django project you should take these steps:
create a new view for your homepage in the main app's views.py
:
from django.shortcuts import render
def homepage(request):
return render(request, 'homepage.html')
then create a new template file homepage.html
for your homepage inside the templates
folder in your main app:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome to My Custom Homepage!</h1>
<!-- Add your homepage content here -->
</body>
</html>
and now update the main app's urls.py
to include a path for the homepage:
from django.contrib import admin
from django.urls import include, path
from . import views import homepage
urlpatterns = [
path('admin/', admin.site.urls),
path("polls/", include("polls.urls")),
path('', homepage, name='home'),
]
now when you navigate to the root URL it will display the custom homepage.
your structure should looks like this:
mysite/
|-- mysite/
| |-- templates/
| |-- mysite/
| |-- homepage.html
|-- polls/
| |-- templates/
| |-- polls/
| |-- index.html
|-- manage.py
and make sure that your TEMPLATES
setting in your main app's settings.py
is configured correctly:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # Add this line
'APP_DIRS': True,
# ...
},
# ...
]
I hope this helps you
Answered By - Kiarash Gh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.