简体   繁体   English

我想将views.py中的值传递给forms.py

[英]I want to pass values are gotten in views.py to forms.py

I want to pass values are gotten in views.py to forms.py. 我想将views.py值传递给forms.py. I wrote in views.py , 我在views.py写的

def app(request):

    if request.method == "POST":
        form = InputForm(data=request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            age = form.cleaned_data['age']

in forms.py 在forms.py中

class InputForm(forms.Form):
    name = forms.CharField(max_length=100)
    age = forms.CharField(max_length=100)

def logic(self):
   #I want to use views.py’s name&age’s value here

I am new to Django, so how should I write codes to do my ideal thing? 我是Django的新手,那么我应该如何编写代码来完成自己理想的事情?

actually you can access data in a form by cleaned_data property (that is a dict object), so as an example checkout save method here: 实际上,您可以通过cleaned_data属性(即dict对象)访问表单中的数据,因此,在此处作为示例checkout save方法:

class CreatePostFrom(forms.Form):
    image = forms.ImageField(required=True)
    des = forms.CharField(required=False, max_length=250)
    location = forms.CharField(required=False, max_length=100)
    tags = forms.CharField(required=False, max_length=250)

    def save(self, user):
        tags = self.cleaned_data["tags"].split()
        for tag in tags:
            Tag.objects.get_or_create(name=tag)
        post = Post.objects.create(user=user,
                                   image=self.cleaned_data["image"],
                                   des=self.cleaned_data["des"],
                                   location=self.cleaned_data["location"])
        post.tags.add(*tags)
        return post

As I said in my comment: 正如我在评论中所说:

Use variables in Form class. 在Form类中使用变量。

class InputForm(forms.Form):
    name = forms.CharField(max_length=100)
    name_data = ""
    age = forms.CharField(max_length=100)
    age_date = 0

    def logic(self):
        # I want to use views.py’s name&age’s value here
        # You can play here with your age_data and name_data

And views.py: 还有views.py:

def app(request):

if request.method == "POST":
    form = InputForm(data=request.POST)
    if form.is_valid():
        form.name_data = form.cleaned_data['name']
        age.name_data = form.cleaned_data['age']

EDIT ( I needed a coffee, sorry.. ) 编辑(我需要一杯咖啡,对不起。)

I have just seen that your cleaned_data come from your form so why not working in it directly like this in form Class: 我刚刚看到您的cleaned_data来自您的表单,所以为什么不直接在类Class中像这样工作:

class InputForm(forms.Form):
    name = forms.CharField(max_length=100)
    age = forms.CharField(max_length=100)

    def logic(self):
        # I want to use views.py’s name&age’s value here
        # Why don't you use cleaned_data['name'] and age from here?
        name = cleaned_data['name']

and simply keep your view like this. 并保持这样的观点。

def app(request):

if request.method == "POST":
    form = InputForm(data=request.POST)
    if form.is_valid():
        name = form.cleaned_data['name']
        age = form.cleaned_data['age']

Not sure this will work at 100% procent 不确定这会以100%的百分比运行

but the goal of this answer is not to give you the exact solution but to let you experiment with your problem a bit. 但是此答案的目的不是给您确切的解决方案,而是让您稍微尝试一下问题。 As a Python beginer you will learn more if you try by yourself and make a few reasearches. 作为Python初学者,如果您自己尝试并进行一些重新搜索,您将学到更多。

Also consider reading this: https://docs.djangoproject.com/en/2.1/topics/forms/#building-a-form-in-django 还可以考虑阅读以下内容: https : //docs.djangoproject.com/en/2.1/topics/forms/#building-a-form-in-django

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM