Created
August 25, 2015 22:28
-
-
Save git2samus/4ba8c9cbfdf8a35132e4 to your computer and use it in GitHub Desktop.
This file contains 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 import forms | |
from students.models import Student, Longlist | |
class StudentForm(forms.ModelForm): | |
class Meta: | |
model = Student | |
fields= ('name_text',) | |
class InputForm(forms.ModelForm): | |
class Meta: | |
model = Longlist | |
fields = ('text_input',) |
This file contains 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
<h1>Enter in a BIG list of names and email addresses of students.</h1> | |
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} | |
<form id="InputForm" method="post" action="/students/textInput/"> | |
{% csrf_token %} | |
{% for hidden in form.hidden_fields %} | |
{{ hidden }} | |
{% endfor %} | |
{% for field in form.visible_fields %} | |
{{ field.errors }} | |
{{ field.help_text }} | |
{{ field }} | |
{% endfor %} | |
<input type="submit" value="Create Students" /> | |
</form> |
This file contains 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
def textInput(request): | |
# A HTTP POST? | |
if request.method == 'POST': | |
form = InputForm(request.POST) | |
# Have we been provided a valid form? | |
if form.is_valid(): | |
# Save the new student list to the database. | |
form.save(commit=True) | |
# Parse the textInput so the name is added to the db | |
l = "" | |
l = Longlist.objects.order_by('-id')[:1] # get the latest entry for Longlist | |
print l | |
# Now call the index() view. | |
# The user will be shown the homepage. | |
return entry(request) | |
else: | |
# The supplied form contained errors | |
print form.errors | |
else: | |
# If the request was not a POST, display the form to enter the details. | |
form = InputForm() | |
# Bad form (or form details), no form supplied... | |
# Render the form with error messages (if any) | |
return render(request, 'students/textInput.html', {'form': form}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment