0

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.

1
  • What is the result? and what are you passing for user? and why are you using request.POST['soss'] when you have form already? Commented Dec 28, 2016 at 19:34

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

so that is why is not passing the data? I should just delete user or add it then? let me give it a try

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.