Skip to content

Instantly share code, notes, and snippets.

@git2samus
Created August 25, 2015 22:28

Revisions

  1. git2samus created this gist Aug 25, 2015.
    16 changes: 16 additions & 0 deletions forms.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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',)
    18 changes: 18 additions & 0 deletions template.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <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>
    28 changes: 28 additions & 0 deletions views.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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})