GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — familiar with form operation · Xu-Guo/python@309535a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • tango_with_django_project
    • README.md
    • db.sqlite3
    • rango
      • forms.py
      • urls.py
      • views.py
    • templates/rango
      • add_category.html
      • add_page.html
      • category.html
      • index.html
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎tango_with_django_project/README.md‎

Copy file name to clipboardExpand all lines: tango_with_django_project/README.md
+5-1Lines changed: 5 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ How To Tango With Django 1.7
77

88
**5**. [Templates and Static Media](http://www.tangowithdjango.com/book17/chapters/templates_static.html)
99

10-
**6**. [Models and Databases](http://www.tangowithdjango.com/book17/chapters/models.html)
10+
**6**. [Models and Databases](http://www.tangowithdjango.com/book17/chapters/models.html)
11+
12+
**7**. [ Models, Templates and Views](http://www.tangowithdjango.com/book17/chapters/models_templates.html)
13+
14+
**8**. [Fun with Forms](http://www.tangowithdjango.com/book17/chapters/forms.html)
Collapse file

‎tango_with_django_project/db.sqlite3‎

Copy file name to clipboard
0 Bytes
Binary file not shown.
Collapse file

‎tango_with_django_project/rango/forms.py‎

Copy file name to clipboard
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from django import forms
2+
from rango.models import Page, Category
3+
import logging
4+
logging.basicConfig(level=logging.DEBUG)
5+
6+
class CategoryForm(forms.ModelForm):
7+
name = forms.CharField(max_length=128, help_text="Please enter the category name.")
8+
views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
9+
likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
10+
slug = forms.CharField(widget=forms.HiddenInput(), required=False)
11+
12+
# An inline class to provide additional information on the form.
13+
class Meta:
14+
# Provide an association between the ModelForm and a model
15+
model = Category
16+
fields = ('name',)
17+
18+
19+
class PageForm(forms.ModelForm):
20+
title = forms.CharField(max_length=128, help_text="Please enter the title of the page.")
21+
url = forms.URLField(max_length=200, help_text="Please enter the URL of the page.")
22+
views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
23+
24+
def clean(self):
25+
cleaned_data = self.cleaned_data
26+
logging.debug("cleaned_data----------{0}".format(cleaned_data))
27+
url = cleaned_data.get('url')
28+
29+
# If url is not empty and doesn't start with 'http://', prepend 'http://'.
30+
if url and not url.startswith('http://'):
31+
url = 'http://' + url
32+
cleaned_data['url'] = url
33+
34+
return cleaned_data
35+
36+
class Meta:
37+
# Provide an association between the ModelForm and a model
38+
model = Page
39+
40+
# What fields do we want to include in our form?
41+
# This way we don't need every field in the model present.
42+
# Some fields may allow NULL values, so we may not want to include them...
43+
# Here, we are hiding the foreign key.
44+
# we can either exclude the category field from the form,
45+
# exclude = ('category',)
46+
# or specify the fields to include (i.e. not include the category field)
47+
fields = ('title', 'url', 'views')
Collapse file

‎tango_with_django_project/rango/urls.py‎

Copy file name to clipboardExpand all lines: tango_with_django_project/rango/urls.py
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
url(r'^$', views.index, name='index'),
88
url(r'^index/', views.index, name='index'),
99
url(r'^about/', views.about, name='about'),
10-
url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.category, name='category'), # New!
10+
url(r'^add_category/$', views.add_category, name='add_category'),
11+
url(r'^add_page/(?P<category_name_slug>[\w\-]+)/$', views.add_page, name='add_page'),
12+
url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.category, name='category'),
1113
]
Collapse file

‎tango_with_django_project/rango/views.py‎

Copy file name to clipboardExpand all lines: tango_with_django_project/rango/views.py
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def category(request, category_name_slug):
6767
# We also add the category object from the database to the context dictionary.
6868
# We'll use this in the template to verify that the category exists.
6969
context_dict['category'] = category
70+
context_dict['category_name_slug'] = category_name_slug
7071

7172
logging.debug('pages-------------{0}'.format(pages))
7273
logging.debug('category-------------{0}'.format(category))
@@ -78,3 +79,59 @@ def category(request, category_name_slug):
7879

7980
# Go render the response and return it to the client.
8081
return render(request, 'rango/category.html', context_dict)
82+
83+
from rango.forms import CategoryForm
84+
85+
86+
def add_category(request):
87+
# A HTTP POST?
88+
if request.method == 'POST':
89+
form = CategoryForm(request.POST)
90+
91+
# Have we been provided with a valid form?
92+
if form.is_valid():
93+
# Save the new category to the database.
94+
form.save(commit=True)
95+
96+
# Now call the index() view.
97+
# The user will be shown the homepage.
98+
return index(request)
99+
else:
100+
# The supplied form contained errors - just print them to the terminal.
101+
print form.errors
102+
else:
103+
# If the request was not a POST, display the form to enter details.
104+
form = CategoryForm()
105+
106+
# Bad form (or form details), no form supplied...
107+
# Render the form with error messages (if any).
108+
return render(request, 'rango/add_category.html', {'form': form})
109+
110+
from rango.forms import PageForm
111+
112+
113+
def add_page(request, category_name_slug):
114+
logging.debug("category_name_slug----------{0}".format(category_name_slug))
115+
try:
116+
cat = Category.objects.get(slug=category_name_slug)
117+
except Category.DoesNotExist:
118+
cat = None
119+
120+
if request.method == 'POST':
121+
form = PageForm(request.POST)
122+
if form.is_valid():
123+
if cat:
124+
page = form.save(commit=False)
125+
page.c_id = cat.c_id
126+
page.views = 0
127+
page.save()
128+
# probably better to use a redirect here.
129+
return category(request, category_name_slug)
130+
else:
131+
print form.errors
132+
else:
133+
form = PageForm()
134+
135+
context_dict = {'form': form, 'category': cat}
136+
137+
return render(request, 'rango/add_page.html', context_dict)
Collapse file

‎tango_with_django_project/templates/rango/add_category.html‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Rango</title>
5+
</head>
6+
7+
<body>
8+
<h1>Add a Category</h1>
9+
10+
<form id="category_form" method="post" action="/rango/add_category/">
11+
12+
{% csrf_token %}
13+
{% for hidden in form.hidden_fields %}
14+
{{ hidden }}
15+
{% endfor %}
16+
17+
{% for field in form.visible_fields %}
18+
{{ field.errors }}
19+
{{ field.help_text }}
20+
{{ field }}
21+
{% endfor %}
22+
23+
<input type="submit" name="submit" value="Create Category" />
24+
</form>
25+
</body>
26+
27+
</html>
Collapse file

‎tango_with_django_project/templates/rango/add_page.html‎

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Rango</title>
5+
</head>
6+
7+
<body>
8+
<h1>Add a Category</h1>
9+
10+
<form id="page_form" method="post" action="/rango/add_page/{{ category.slug }}/">
11+
<input type="hidden" value="{{ category.slug }}">
12+
{% csrf_token %}
13+
{% for hidden in form.hidden_fields %}
14+
{{ hidden }}
15+
{% endfor %}
16+
17+
{% for field in form.visible_fields %}
18+
{{ field.help_text }} :
19+
{{ field }} {{ field.errors }}<br />
20+
{% endfor %}
21+
22+
<input type="submit" name="submit" value="Create Category" />
23+
</form>
24+
</body>
25+
26+
</html>
Collapse file

‎tango_with_django_project/templates/rango/category.html‎

Copy file name to clipboardExpand all lines: tango_with_django_project/templates/rango/category.html
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ <h1>{{ category_name }}</h1>
1919
{% else %}
2020
The specified category {{ category_name }} does not exist!
2121
{% endif %}
22+
<a href="/rango/add_page/{{ category_name_slug }}/">Add a New Page</a><br />
2223
</body>
2324
</html>
Collapse file

‎tango_with_django_project/templates/rango/index.html‎

Copy file name to clipboardExpand all lines: tango_with_django_project/templates/rango/index.html
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ <h2>Top 5 Page</h2>
3030
<strong>There are no pages present.</strong>
3131
{% endif %}
3232

33+
<a href="/rango/add_category/">Add a New Category</a><br />
3334

3435
</body>
3536
</html>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.