Django简单的Post请求不起作用

时间:2017-09-25 17:07:25

标签: django python-3.x django-forms django-templates django-views

我是Django的新手并尝试构建一个简单的网页。我正在尝试发布请求,但没有值插入到数据库中。我希望问题应该在views.py forms.is_valid()中,因为在此行之后没有记录日志。请协助

model.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>

1 个答案:

答案 0 :(得分:1)

您在模板和视图中使用了错误的名称属性值,因此请根据以下代码进行更新

<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>

Views.py

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', '')