3

これは私のモデルです:

from django.db import models

class Meeting(models.Model):
    name = models.CharField(max_length=255)
    time = models.DateTimeField()
    confirmed = models.BooleanField(default=False)

これは私のフォームです:

from django import forms

class MeetingForm(forms.Form):
    name = forms.CharField(max_length=100)
    time = forms.DateTimeField()
    user_name = forms.CharField(max_length=100)
    user_email = forms.EmailField()

リンク付きのメールをユーザーに送信するビューを作成する方法と、ユーザーがこのリンクをクリックすると、確認済みフィールドの値が true に変更されますか?

リンクは私の最大の問題です。

4

1 に答える 1

3

このようなことができます

モデル

import random
import string

class Meeting(models.Model):
    name = models.CharField(max_length=255)
    time = models.DateTimeField()
    confirmed = models.BooleanField(default=False)
    key = models.CharField(max_length=32) 

    def save(self, *args, **kwargs):
        '''
        On save generate key which will be sent in email
        '''
        self.key = ''.join(random.choice(string.letters) for i in xrange(32))
        super(Meeting, self).save(*args, **kwargs)
        return self

ビュー.py

def confirm(request, key):
    try:
        meeting = Meeting.objects.get(key=key)
    except Meeting.DoesNotExist:
        # wrong key, do something, redirect to somewhere etc
        template = 'error.html'
    else:
        meeting.confirmed = True
        meeting.save()
        template = 'success.html'
    render(request, template)

def send_email(request):
    email = request.user.email
    meeting = Meeting.objects.get(...your condition here...)
    send_mail('Confirmation email',
              # this is a link you wanted to
              '<a href="%s">confirm</a>' % reverse('user_confirm',
                                                   kwargs={'key': meeting.key}),
              'from@youremail.com', [email])
    render(request, 'mailsent.html')

urls.py

urlpatterns = patterns('',
    url(r'^confirm/(?P<key>[\w]{32})/$', 'yourapp.views.confirm',
        name='user_confirm'),
    )
于 2012-09-18T19:52:04.577 に答える