关闭窗口或用户注销后自动删除用户输入的数据

时间:2019-06-18 06:06:53

标签: python django django-views

我有一个应用程序,用户可以在其中输入数据,并通过Djnago表单将其保存在数据库中。但是我不想永远保存此用户输入的数据,仅在用户登录之前保存。一旦用户注销或关闭浏览器,我希望Djnago删除所有该用户输入的数据。我不想通过外键将此模型与用户关联,因为对我来说很难通过Django表单进行配置。

请查看代码,并告诉我如何实现此目标。我听说过有关数据库的AutoCommit = False

views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import *
from django.contrib.admin.views.decorators import staff_member_required
from .forms import *
from django.shortcuts import *
from .models import *
from django.contrib.auth.forms import *
from django.contrib.auth import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.views.generic import CreateView
from django.views import generic
from .models import *






def reg_user(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            return redirect('LoginPage')
    else:
        form = UserCreationForm()
    return render(request, 'userfiles/reg.html', {'form': form})





Also there is an issue here. Whenever I use the following decorator I get this error


  File "C:\Users\Bitswits 3\Desktop\Maala\MaalaWeddings\userfiles\urls.py", line 22, in <module>
    url(r'^invite/$', InviteCreate.as_view(), name='Invite-Page'),le "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-package
AttributeError: 'function' object has no attribute 'as_view'




# @login_required(login_url='LoginPage')
class InviteCreate(CreateView):
    form_class = InviteForm
    model = Invite
    template_name = "userfiles/Invite.html"

    def get_success_url(self):
            return reverse('Invite-Page')

urls.py

from django.conf.urls.static import static
from django.conf import settings
from django.conf.urls import url
from . import views
from django.views.generic import *
from django.views import generic
from django.contrib.auth import views as auth_views

from .views import (
    InviteCreate

)


urlpatterns = [


    url(r'^invite/$', InviteCreate.as_view(), name='Invite-Page'),

    url(r'^changepassword/$', views.change_password, name='Pass-Change'),

    url(r'^(?i)registration/$', views.reg_user, name='Reg-Page'),


    url(r'^(?i)login/$', auth_views.LoginView.as_view(
        template_name='userfiles/login.html'), name='LoginPage'),



] 

models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models



class Invite(models.Model):


    invite_first_name = models.CharField(
        "First Name", default="", max_length=35)
    invite_last_name = models.CharField(
        "Last Name", default="", max_length=35)
    invite_msg = models.TextField("Invitation Message")


1 个答案:

答案 0 :(得分:0)

要在类基础视图上使用login_required,请查看文档read_json

我想邀请是用来让用户邀请其他人加入您的Django应用程序的吗?如果是这样,您可以在uuid字段中输入Invite,并在邀请电子邮件中使用指向URL中uuid的注册的链接。因此,当有人使用邀请uuid注册时,您将删除该记录。

我不知道您希望邀请有效多长时间,但是您也可以添加一个过期的datetime字段并定期删除所有过期的邀请