Created
January 21, 2019 15:29
-
-
Save glasgowm148/8001bce6903e48df8ab45bc33bbaa837 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
{% load staticfiles %} | |
<html> | |
<head> | |
<title> Rango - | |
{% block title_block %} | |
How to Tango with Django! | |
{% endblock %} | |
</title> | |
</head> | |
<body> | |
<div> | |
{% block body_block %} | |
{% endblock %} | |
</div> | |
<hr /> | |
<div> | |
<ul> | |
<li><a href="{% url 'add_category' %}">Add New Category</a></li> | |
<li><a href="{% url 'about' %}">About</a></li> | |
<li><a href="{% url 'index' %}">Index</a></li> | |
</ul> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NoReverseMatch at /rango/about/ | |
Reverse for 'add_category' not found. 'add_category' is not a valid view function or pattern name. | |
Request Method: GET | |
Request URL: http://127.0.0.1:8000/rango/about/ | |
Django Version: 1.11.17 | |
Exception Type: NoReverseMatch | |
Exception Value: | |
Reverse for 'add_category' not found. 'add_category' is not a valid view function or pattern name. | |
Exception Location: /Users/markglasgow/anaconda3/envs/rango/lib/python3.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 497 | |
Python Executable: /Users/markglasgow/anaconda3/envs/rango/bin/python3 | |
Python Version: 3.7.2 | |
Python Path: | |
['/Users/markglasgow/Documents/GitHub/semtwo/WAD/2018/Workspace/tango_with_django_project', | |
'/Users/markglasgow/anaconda3/envs/rango/lib/python37.zip', | |
'/Users/markglasgow/anaconda3/envs/rango/lib/python3.7', | |
'/Users/markglasgow/anaconda3/envs/rango/lib/python3.7/lib-dynload', | |
'/Users/markglasgow/anaconda3/envs/rango/lib/python3.7/site-packages'] | |
Server time: Mon, 21 Jan 2019 15:23:45 +0000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
### | |
# - rango URLs | |
### | |
### | |
from django.conf.urls import url | |
from rango import views | |
app_name = 'rango' | |
urlpatterns = [ | |
url(r'^$', views.index, name='index'), | |
url(r'^about/$', views.about, name='about'), | |
url(r'^add_category/$', views.add_category, name='add_category'), | |
url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.show_category, name='show_category'), | |
url(r'^category/(?P<category_name_slug>[\w\-]+)/add_page/$', views.add_page, name='add_page'), | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
# Import the Category model | |
from rango.forms import CategoryForm | |
from rango.models import Category | |
from rango.models import Page | |
from rango.forms import PageForm | |
def index(request): | |
category_list = Category.objects.order_by('-likes')[:5] | |
page_list = Page.objects.order_by('-views')[:5] | |
context_dict = {'categories': category_list, 'pages': page_list} | |
return render(request, 'rango/index.html', context=context_dict) | |
def about(request): | |
return render(request, 'rango/about.html') | |
def show_category(request, category_name_slug): | |
context_dict = {} | |
try: | |
category = Category.objects.get(slug=category_name_slug) | |
pages = Page.objects.filter(category=category) | |
context_dict['pages'] = pages | |
context_dict['category'] = category | |
except Category.DoesNotExist: | |
context_dict['category'] = None | |
context_dict['pages'] = None | |
return render(request, 'rango/category.html', context_dict) | |
def add_category(request): | |
form = CategoryForm() | |
# A HTTP POST? | |
if request.method == 'POST': | |
form = CategoryForm(request.POST) | |
# Have we been provided with a valid form? | |
if form.is_valid(): | |
# Save the new category to the database. | |
form.save(commit=True) | |
# Now that the category is saved | |
# We could give a confirmation message | |
# But since the most recent category added is on the index page # Then we can direct the user back to the index page. | |
return index(request) | |
else: | |
# The supplied form contained errors - # just print them to the terminal. | |
print(form.errors) | |
# Will handle the bad form, new form, or no form supplied cases. # Render the form with error messages (if any). | |
return render(request, 'rango/add_category.html', {'form': form}) | |
def add_page(request, category_name_slug): | |
try: | |
category = Category.objects.get(slug=category_name_slug) | |
except Category.DoesNotExist: | |
category = None | |
form = PageForm() | |
if request.method == 'POST': | |
form = PageForm(request.POST) | |
if form.is_valid(): | |
if category: | |
page = form.save(commit=False) | |
page.category = category | |
page.views = 0 | |
page.save() | |
return show_category(request, category_name_slug) | |
else: | |
print(form.errors) | |
context_dict = {'form':form, 'category': category} | |
return render(request, 'rango/add_page.html', context_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment