[Answer]-Django Form Submission Failing

1👍

Your first URL pattern, new, is not anchored to the start of the string. That means that it matches anything that ends with “new” – and that includes “savenew”. So your request for “savenew” is being caught by that pattern, and being sent to the new view.

Just put a ^ character at the front, as you have done with the other pattern.

0👍

try to use Modelforms

forms.py:

from django.forms import ModelForm
from myapp.models import Location

# Create the form class.

class LocationForm(ModelForm):
     class Meta:
         model = Location

view.py

def savenew(request):
if request.method == 'POST':
    form = LocationForm(request.POST)
    if form.is_valid():

        new=form.save()

        return HttpResponseRedirect(reverse(reverse('locationlib:detail', args=[new.id])))
    return render(request,'reservetion/sign.html',{'form': form})
else:
    form = SignForm()
    return render(request, 'reservetion/sign.html',{'form': form})         

new.html

  <form action="{% url 'locationlib:savenew' %}" method="post">
    {% csrf_token %}
     {{ form}}
</form>

Leave a comment