0
我是Django的新手,嘗試構建簡單的網頁。我試圖發佈請求,但沒有值插入到數據庫中。我希望問題應該在views.py forms.is_valid()由於沒有日誌記錄在此行後。請幫助Django簡單發佈請求不起作用
模型。 PY
from django.db import models
from django.contrib.auth.models import User
from django.db.models import Q
from django.forms import ModelForm
from django import forms
# Create your models here.
class aws_cred(models.Model):
aws_access_user_id = models.ForeignKey(User,null=False,related_name="aws_acc_user")
access_key = models.CharField(max_length=300)
secret_key = models.CharField(max_length=300)
class aws(ModelForm):
class Meta:
model = aws_cred
fields = ['access_key','secret_key','aws_access_user_id']
views.py
from django.shortcuts import render_to_response,HttpResponseRedirect,render,redirect,reverse
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from s3comp.models import aws_cred,aws
import logging
@login_required
def fileinput(req):
logging.basicConfig(filename='log_filename.txt',level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
if req.method == 'POST':
form = aws(req.POST)
logging.debug(form)
try:
logging.debug('step 4')
if form.is_valid():
logging.debug('step 5')
access_key_val = req.POST.get('access key','')
secret_key_val = req.POST.get('secret key','')
aws_access_user_id_val = req.POST.get('aws access user id', '')
logging.debug(access_key_val+" " +secret_key_val+" " +aws_access_user_id_val)
cred_obj = aws(access_key = access_key_val,secret_key =secret_key_val,aws_access_user_id = aws_access_user_id_val)
cred_obj.save()
return HttpResponseRedirect(reverse('s3comp:fileinput'))
except Exception as e:
logging.debug(e)
else:
form = aws()
return render(req,'s3comp/fileinput.html',{'form':form})
HTML文件
<form action="{% url 'fileinput_home' %}" method="post">
{% csrf_token %}
<p><label for="Aws access user id">User:</label><input type="text" name="Aws access user id" value={{ user.get_username }}/></p>
<p><label for="Access Key">Access Key:</label><input type="text" name="Access Key"/></p>
<p><label for="Secret Key">Secret Key:</label><input type="text" name="Secret Key"/></p>
<input type="submit" value="Submit">
</form>
謝謝爲了您的迴應。我修改了它,但它仍然沒有反映在數據庫中。它仍然沒有在行後移動如果form.is_valid(): – user2376622
你也有錯誤的代碼保存在數據庫中的條目。 –