Skip to content

Instantly share code, notes, and snippets.

@eclecticmiraclecat
Last active November 4, 2020 14:34
Show Gist options
  • Save eclecticmiraclecat/2a27b7879f585a18fa4114fadeb2de71 to your computer and use it in GitHub Desktop.
Save eclecticmiraclecat/2a27b7879f585a18fa4114fadeb2de71 to your computer and use it in GitHub Desktop.
# urls.py
from .views import display, onsubmit

urlpatterns = [
    path('hello/', display, name='n_index'),
    path('onsubmit/', onsubmit, name='n_onsubmit'),
]
# views.py
def display(request):
  form = UserRegistrationForm()
  if request.method=='POST':
    form = UserRegistrationForm(request.POST)
    if form.is_valid():
      print(form.cleaned_data['firstName'])
      print(form.cleaned_data['lastName'])
      print(form.cleaned_data['email'])
    return HttpResponseRedirect(reverse('n_onsubmit'))
  return render(request, 'firstApp/index.html', {'form': form})

def onsubmit(request):
  form = UserRegistrationForm()
  return render(request, 'firstApp/submit.html', {'form': form}
<!-- index.html -->
<form action="{% url 'n_index' %}" method="post">
  {{ form.as_p }}
  {% csrf_token %}
  <input type="submit" value="Submit">
</form>
<!-- submit.html -->
submitted

<a href="{% url 'n_index' %}">Register Again?</a>

notes

  • reverse is just pointing back to the url
# urls.py
path('onsubmit/', onsubmit, name='n_onsubmit')
>>> from django.urls import reverse
>>> reverse('n_onsubmit')
'/onsubmit/'

HttpResponseRedirect

  • is disabling browser refresh to resend the form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment