0

I'm trying to get the users input and add to a table in the database, everything goes smoothly with no errors .. but the input is not added to DB

urls.py

path('category/add/', views.add_cat, name="add_cat"),

view.py

def add_cat(request):
        # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = CatForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            entry = Categories.objects.create(category_name=new_cat_name)
            entry.save()
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = CatForm()

    return render(request, 'add_cat.html', {'form': form})

add_cat.html

{% extends 'base.html' %}
    {% block content %}
{% load static %}
<form action="/" method="post">
    {% csrf_token %}
    {% for form in form %}
    <h3 align="center">{{ form.label }}</h3>
    <div align="center">{{ form }}</div>
    <br>
    <br>
    {% endfor %}
    <div align="center">
    <input type="submit" class="btn btn-dark" style="width: 100px;"value="إضافة" />
</div>
</form>
{% endblock %}

1 Answer 1

1
{% extends 'base.html' %}
    {% block content %}
{% load static %}
<form action="" method="post">
    {% csrf_token %}
    {% for form in form %}
    <h3 align="center">{{ form.label }}</h3>
    <div align="center">{{ form }}</div>
    <br>
    <br>
    {% endfor %}
    <div align="center">
    <input type="submit" class="btn btn-dark" style="width: 100px;"value="إضافة" />
</div>
</form>
{% endblock %}

change your form action, you were posting the data to '/' url but you need to put it in your add view

if form.is_valid():
        form.save()
        return HttpResponseRedirect('/')
Sign up to request clarification or add additional context in comments.

5 Comments

That's is great ,it worked but now i changed the variable to entry = Categories.objects.create(category_name=form) adding the form to the end of the line, the whole HTML line for the form is added as str to database
check the edited ans, and dont forget to upvote and accept the ans if helped
returned the error 'CatForm' object has no attribute 'save'
is your form a ModelForm or normal Form?
As I am very new to Django, if I get your question right. I think its normal form because it is not created from models.py, only the DB table did, according to docs.djangoproject.com/en/2.0/topics/forms/modelforms , it is normal form and i need to change it to model form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.