Deluq
Deluq

Reputation: 211

Django Saving Data into Database

I'm having an issue, what I need is to save a part number into a database table. So everytime a user enters the SOSS it should be save in my table. This is my code but is not saving anything, not sure what I'm doing wrong.

manifiestos.html

                                            <form action="{%  url 'manifiestos' %}" method="post"> {% csrf_token %}
                                                <p><label for="date"> Date:</label> <input type="text" name="date" value={% now "Y-m-d" %} /> </p>
                                                <p><label for="soss"> SOSS:</label> <input type="text" name="soss" id="soss" /> </p>
                                                <input type="submit" value="Submit" />
                                        </form>

models.py

class manifiestos_bts(models.Model):
    soss = models.CharField(max_length=50)
    date = models.DateTimeField(null=True, blank=True)
    user = models.CharField(max_length=50)

forms.py

class ManifiestosForm(forms.Form):
    soss = forms.CharField()
    date = forms.DateTimeField()
    user = forms.CharField()

html_views

@login_required(login_url='/msr/login')
def manifiestos(request):
    if request.method == 'POST':

        form = ManifiestosForm(request.POST)
        if form.is_valid():
            soss = request.POST.get('soss', '')
            date = request.POST.get('date', '')
            manifiestos_obj = manifiestos_bts(soss= soss, date= date)
            manifiestos_obj.save()

            return  HttpResponseRedirect(reverse('manifiestos'))
    else:
        form = ManifiestosForm()

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

urls.py

    url(r'^manifiestos$', html_views.manifiestos, name='manifiestos'),

Thanks for your time :) If you need more details just let me know.

Upvotes: 0

Views: 2228

Answers (1)

mukul
mukul

Reputation: 111

Your form.is_valid() will fail because you are not passing user from your template. Either remove it from ManifiestosForm or pass it from manifiestos.html

Upvotes: 2

Related Questions