Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
csev committed Oct 2, 2019
1 parent 33edc2b commit 8ce7dbd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
7 changes: 2 additions & 5 deletions form/forms.py
@@ -1,16 +1,13 @@

from django import forms
from django.core.exceptions import ValidationError
from django.core import validators

class BasicForm(forms.Form):
title = forms.CharField(validators=[validators.MinLengthValidator(2, "Please enter 2 or more characters")])
title = forms.CharField(validators=[
validators.MinLengthValidator(2, "Please enter 2 or more characters")])
mileage = forms.IntegerField()
purchase_date = forms.DateField()




# References

# https://docs.djangoproject.com/en/2.1/ref/forms/api/
Expand Down
6 changes: 3 additions & 3 deletions form/templates/form/dump.html
@@ -1,8 +1,8 @@
<html><body>
<p>Dump of {{ title }}:
<pre>
{{ dump }}
</pre>
<p>
{{ dump|safe }}
</p>
</p>
<a href="{% url 'form:main' %}">Back</a>
</body></html>
4 changes: 3 additions & 1 deletion form/templates/form/form.html
Expand Up @@ -5,6 +5,8 @@
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
<input type="submit" onclick="window.location='{% url 'form:main' %}' ; return false;" value="Cancel">
<input type="submit"
onclick="window.location='{% url 'form:main' %}' ; return false;"
value="Cancel">
</form>
</p>
2 changes: 1 addition & 1 deletion form/templates/form/main.html
Expand Up @@ -6,7 +6,7 @@
<p>This page is coming from a file in samples/form/templates/main_form.html</p>
<p>
<ul>
<li><a href="example">Render only view</a></li>
<li><a href="example">Raw output of form.as_table</a></li>
<li><a href="create">Simple create view</a></li>
<li><a href="update">Simple update view</a></li>
<li><a href="validate">Form validation</a>
Expand Down
21 changes: 14 additions & 7 deletions form/views.py
Expand Up @@ -3,18 +3,26 @@
from django.views import View
from django.urls import reverse
from form.forms import BasicForm
import html

import json

# Create your views here.
def example(request) :
form = BasicForm()
return HttpResponse(form.as_table())

# Call as dumpdata('GET', request.GET)
def dumpdata(place, data) :
retval = ""
if len(data) > 0 :
retval += '<p>Incoming '+place+' data:<br/>\n'
for key, value in data.items():
retval += html.escape(key) + '=' + html.escape(value) + '</br>\n'
retval += '</p>\n'
return retval

class DumpPostView(View): # Reusable bit...
def post(self, request) :
js = json.dumps(request.POST, sort_keys=True, indent=4)
ctx = {'title': 'request.POST', 'dump': js}
dump = dumpdata('POST', request.POST)
ctx = {'title': 'request.POST', 'dump': dump}
return render(request, 'form/dump.html', ctx)

class SimpleCreate(DumpPostView):
Expand Down Expand Up @@ -50,8 +58,7 @@ def post(self, request) :
if not form.is_valid() :
ctx = {'form' : form}
return render(request, 'form/form.html', ctx)
# Save the Data
# Look up the url for the next view in urls.py
# If there are no errors, we would save the data
x = reverse('form:success')
return redirect(x)

Expand Down

0 comments on commit 8ce7dbd

Please sign in to comment.