如何在一个Just View中调用几个函数

时间:2015-10-22 23:59:42

标签: python django

好吧,我是新手使用Python和Django然后,我想知道如何在一个视图中调用多个功能,例如我正在做一个应用程序,人们购买服务器,并收到通知,然后我想打电话给函数来完成所有这一切没有问题,我做登录工作,但注册没有,和通知工程当我禁用表单调用进行注册,因为我想在同一页面中的所有这些事情,例如在 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 ,但在视图中只能调用registro或notificaciones,我希望知道我是怎么回事包括两者和两件作品。

1 个答案:

答案 0 :(得分:2)

我认为你有一些错误的概念。模板(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 %}