Skip to main content
 首页 » 编程设计

python之如何在一个 View 中调用多个函数

2024年10月24日1698°冷暖

好吧,我是 Python 和 Django 的新手,我想知道如何在 View 中调用多个函数,例如我正在开发一个人们购买服务器并接收通知的应用程序,然后我想调用所有这些功能都没有问题,我登录 Works 但没有注册,当我禁用表单调用以进行注册时通知有效,因为我希望所有这些都在同一页面中,例如在 Layout.html,就像base,现在看起来是我的观点

Views.py

def index(request): 
    if request.method == 'POST': 
        form = RegistroUserForm(request.POST, request.FILES) 
    else: 
        form = RegistroUserForm() 
    context = { 
        'form': form 
    } 
    notifi = Notificaciones.objects.all() 
    return render(request,'app/index.html',context) 
 
def registro_usuario_view(request): 
    if request.method == 'POST': 
        # Si el method es post, obtenemos los datos del formulario 
        form = RegistroUserForm(request.POST, request.FILES) 
 
        # Comprobamos si el formulario es valido 
        if form.is_valid(): 
            # En caso de ser valido, obtenemos los datos del formulario. 
            # form.cleaned_data obtiene los datos limpios y los pone en un 
            # diccionario con pares clave/valor, donde clave es el nombre del campo 
            # del formulario y el valor es el valor si existe. 
            cleaned_data = form.cleaned_data 
            username = cleaned_data.get('username') 
            password = cleaned_data.get('password') 
            email = cleaned_data.get('email') 
            # E instanciamos un objeto User, con el username y password 
            user_model = User.objects.create_user(username=username, password=password) 
            # Añadimos el email 
            user_model.email = email 
            # Y guardamos el objeto, esto guardara los datos en la db. 
            user_model.save() 
            # Ahora, creamos un objeto UserProfile, aunque no haya incluido 
            # una imagen, ya quedara la referencia creada en la db. 
            user_profile = UserProfile() 
            # Al campo user le asignamos el objeto user_model 
            user_profile.user = user_model 
            # Por ultimo, guardamos tambien el objeto UserProfile 
            user_profile.save() 
    else: 
        # Si el mthod es GET, instanciamos un objeto RegistroUserForm vacio 
        form = RegistroUserForm() 
    # Creamos el contexto 
    context = {'form': form} 
    # Y mostramos los datos 
    return render(request, 'app/registro.html', context) 

然后在我的 Layout.html 中,我包括 registro.html 和 notificaciones.html,但在 View 中只能调用 registro 或 notificaciones,我想知道我是怎么做的包括两者和两个作品。

请您参考如下方法:

我认为你有一些错误的概念。模板 (Layout.html) 不调用任何函数。 views.py 中的代码使用模板创建一个将由浏览器处理的完整 HTML 页面

你的两个函数共享很多代码,除了第一个函数中的这一行都保持不变:

notifi = Notificaciones.objects.all() 

您可能想要摆脱 def index() 并将上一行移动到 def registro_usuario_view()。

如果我没理解错的话,你想在模板中呈现 Notificaciones。但是您没有将 notifi 传递给任何地方的渲染器。您必须将其添加到上下文中。例如:

notifications = Notifications.objects.all() 
 
context = {"form": form, 
           "notifications": notifications} 

然后在您的模板中,您可以访问 {{ notifications }} 及其字段和方法,比方说:

{% for notification in notifications %} 
    In {{ notifications.date }}, {{ notifications.author }} said: 
    {{ notifications.message }} 
{% endfor %}