0

I have an app, where the user enters data and it is saved in the db through Djnago forms. But I don't want to save this user entered data forever, only until the user is logged in. As soon as the user logs out or closes his browser I want Djnago to delete all that user entered data. I don't want to associate this Model with the User through Foreignkey as It's hard to configure through Django forms for me.

Please look at the code and tell me how can I achieve this. I heard about AutoCommit = False about the db.

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")


Talha Murtaza
  • 324
  • 1
  • 2
  • 17

1 Answers1

0

For using login_required on class base view take a look at the docs https://docs.djangoproject.com/en/2.2/topics/class-based-views/intro/#decorating-the-class

I guess the invite is used to let a user invite other people to your Django app? If so you could at uuid field to Invite and in the invite email with a link to the registration with the uuid in the url. So when somebody register with an invite uuid you remove that record.

I don't know for how long you want the invite to be valid, but you could also add a an expired datetime field and periodically remove all expired invites

Krukas
  • 657
  • 3
  • 10
  • No the basic idea is. The user creates a _wedding invite_ ,which is rendered into a basic html template and the user could print this page as an invitation card. After the user logs out or even if the user closes the browser window. I want the Invite table to be removed and return to 0(No items). No particular time period is set for keeping the data, as soon as the user shuts the browser window or logs out data should be removed. – Talha Murtaza Jun 18 '19 at 11:07
  • Why don't you store that information in browser only, and send it to the invitation card page. You could store it in a cookie with a short valid time and as long the page is open you update it. As soon as the user closes the page it will expire after x time. – Krukas Jun 18 '19 at 11:29
  • How? I couldn't find anything useful in Google. Could you please share a link etc – Talha Murtaza Jun 18 '19 at 11:36
  • Here is some simple code to for get/set cookies https://stackoverflow.com/a/24103596/6682340 and you could use https://www.w3schools.com/jsref/met_win_setinterval.asp to update cookie. And some info about how to set/get value from input field https://www.w3schools.com/jsref/prop_text_value.asp. I think with those links you have the information to create a simple JS script. – Krukas Jun 18 '19 at 11:44
  • But how will cookies delete the data? – Talha Murtaza Jun 18 '19 at 11:58
  • If you look at the example of the set/get cookie you will see that also an `expires` if you make this for example 5\10 minutes in future it will automatically be removed by browser after 5\10 minutes – Krukas Jun 18 '19 at 12:00
  • Nothing useful I could find in it. – Talha Murtaza Jun 18 '19 at 12:28
  • I want to achieve this inside Django – Talha Murtaza Jun 18 '19 at 12:28