GE02 ‐ 3 ‐ Define URI Path and View (Abi Drennan) - wycre/CS3300-Team-2-9 GitHub Wiki
- Open
django_project urls.pyto add a path below the admin path to include the specific urls that will be created in theportfolio_app urls.py. You need to import include:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
#connect path to portfolio_app urls
path('', include('portfolio_app.urls')),
]
- Update
portfolio_app/views.pyby defining the following view for the home page:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
# Render the HTML template index.html with the data in the context variable.
return HttpResponse('home page')
- Create
urls.pyfile inportfolio_appthat contains a path to the defined view:
from django.urls import path
from . import views
urlpatterns = [
#path function defines a url pattern
#'' is empty to represent based path to app
# views.index is the function defined in views.py
# name='index' parameter is to dynamically create url
# example in html <a href="{% url 'index' %}">Home</a>.
path('', views.index, name='index'),
]
- Run start server command:
python manage.py runserver
- Open http://127.0.0.1:8000/ and you should see home page:
- Update
portfolio_app/views.pyviews by defining the following view for the home page:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
# Render index.html
return render( request, 'portfolio_app/index.html')
-
- What do you think is the reason for the issue? In the next part you will fix it.
-
Version on your
Sprint01branch and update your remote repository:
# add the changes you've made your project files to the staging area
git add .
# commit the changes with a descriptive commit message
git commit -m "Updated views for homepage"
# push the committed change to your remote repository
git push origin Sprint01