简体   繁体   English

Django:如何在其他python脚本中使用表单值?

[英]Django: how do I use the form values in other python scripts?

this is my views.py 这是我的views.py

from django.shortcuts import render
from .reisplanner import *

def home(request):
    return render(request, 'home.html', {})

def reisplanner(request):
    if request.method == 'POST':
        form = reisplannerForm(request.POST)

    if form.is_valid():
        beginstation = form.cleaned_data['beginstation']
        eindstation = form.cleaned_data['eindstation']

        print(beginstation, eindstation)
    else:
        form = reisplannerForm()

    return render(request, 'reisplanner.html', {'form': form})

This is forms.py 这是forms.py

from django import forms

class reisplannerForm(forms.Form):
    beginstation = forms.CharField(required=True)
    eindstation = forms.CharField(required=True)

and i want to use the variables in reisplanner.py 我想使用reisplanner.py的变量

def reisplanner():
    #
    # use the values of the form here
    #

I have literally tried everything, this is for a school project and have been trying to fix this for about 2 days now with no result. 我实际上已经尝试了一切,这是针对一个学校项目的,并且已经尝试修复了大约2天,但没有结果。

If you guys could help me that would be fantastic. 如果你们能帮助我,那太好了。 how do I get the values of a form to my python script? 如何获取表单值到python脚本?

In reisplanner.py reisplanner.py

def my_reisplanner(form):
    beginstation = form.cleaned_data['beginstation']
    eindstation = form.cleaned_data['eindstation']
    # Do somthing with the values

In views.py views.py

from django.shortcuts import render
from .reisplanner import *

def home(request):
    return render(request, 'home.html', {})

def reisplanner(request):
    if request.method == 'POST':
        form = reisplannerForm(request.POST)

    if form.is_valid():
        beginstation = form.cleaned_data['beginstation']
        eindstation = form.cleaned_data['eindstation']

        print(beginstation, eindstation)

        # You can call this function because you imported it in line 2
        my_reisplanner(form)
    else:
        form = reisplannerForm()

    return render(request, 'reisplanner.html', {'form': form})

The only problem here is the name of the function to call. 唯一的问题是要调用的函数的名称。 If you use the same name for the view function and the process function, you will have issue with imports. 如果对视图函数和过程函数使用相同的名称,则导入时会出现问题。

In the previous snippet, I renamed the process function to my_reisplanner to avoid any clash. 在上一个代码片段中,我将流程函数重命名为my_reisplanner以避免任何冲突。 There is other solutions if you don't want (or can't) rename the process function (see after). 如果您不想(或无法)重命名流程功能(请参阅下文),则还有其他解决方案。

In general, this kind of import is discouraged 一般来说,不鼓励这种进口

from .reisplanner import *

as you can't control what is imported. 因为您无法控制导入的内容。 Prefer this: 喜欢这个:

from .reisplanner import reisplanner as reisplanner_name_alias
# [...]
# Call reisplanner using local alias created from imports
reisplanner_name_alias()

or simply 或简单地

from . import reisplanner
# [...]
# Call reisplanner using full modul dotted path
reisplanner.reisplanner()

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

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