Ich habe ein Problem, was ich brauche ist eine Teilenummer in eine Datenbanktabelle zu speichern. Jedes Mal, wenn ein Benutzer den SOSS eingibt, sollte er in meiner Tabelle gespeichert werden. Dies ist mein Code, aber spart nichts, nicht sicher, was ich falsch mache.Django Speichern von Daten in Datenbank
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'),
Vielen Dank für Ihre Zeit :) Wenn Sie weitere Informationen brauchen mich nur wissen lassen.
Was ist das Ergebnis? und was passierst du für "user"? und warum benutzt du 'request.POST ['soss']' wenn du schon 'form' hast? – kia