Django Context теряется в электронной почте
Я пытаюсь отправить письмо о забытом пароле в Django. Но даже после использования отладочных операторов print, чтобы убедиться, что slug доступен в моем контексте, он продолжает выдавать ошибку.
Мой models.py:
class Restaurant(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
description = models.TextField()
landing_page_tagline = models.TextField()
landing_page_image = models.ImageField(upload_to='restaurant_images/')
address = models.CharField(max_length=255, null=True)
phone = models.CharField(max_length=20, null=True)
logo_image = models.ImageField(upload_to='restaurant_images/')
primary_color = models.CharField(max_length=7) # Hex color code
favicon = models.FileField(upload_to='restaurant_favicons/', null=True, blank=True)
about_us_image1 = models.ImageField(upload_to='restaurant_images/')
about_us_text1 = models.TextField(blank=True, null=True)
about_us_image2 = models.ImageField(upload_to='restaurant_images/')
about_us_text2 = models.TextField(blank=True, null=True)
about_us_image3 = models.ImageField(upload_to='restaurant_images/')
about_us_text3 = models.TextField(blank=True, null=True)
contact_us_image = models.ImageField(upload_to='restaurant_images/')
map_iframe_src = models.TextField(blank=True, null=True)
footer_text = models.TextField()
facebook_link = models.URLField(null=True, blank=True)
instagram_link = models.URLField(null=True, blank=True)
youtube_link = models.URLField(null=True, blank=True)
twitter_link = models.URLField(null=True, blank=True)
slug = models.SlugField(unique=True, max_length=100, null=True)
stripe_subscription_id = models.CharField(max_length=255, blank=True, null=True)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
super().save(*args, **kwargs)
def __str__(self):
return self.name
class RestaurantUser(AbstractUser):
restaurant = models.OneToOneField('Restaurant', on_delete=models.CASCADE, null=True, blank=True)
phone = models.CharField(max_length=20, blank=True, null=True)
is_restaurant_admin = models.BooleanField(default=False)
groups = models.ManyToManyField(
Group,
related_name='restaurantuser_set',
blank=True,
help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.',
related_query_name='restaurantuser',
)
user_permissions = models.ManyToManyField(
Permission,
related_name='restaurantuser_set',
blank=True,
help_text='Specific permissions for this user.',
related_query_name='restaurantuser',
)
def save(self, *args, **kwargs):
if self.is_restaurant_admin:
self.is_staff = True
super().save(*args, **kwargs)
Мой views.py:
Мой urls.py:
path('restaurant/<slug:slug>/', views.restaurant_landing_page_view, name='restaurant_landing_page'),
path('password-reset/<slug:restaurant_slug>/', CustomPasswordResetView.as_view(), name='password_reset'),
path('password-reset-done/<slug:restaurant_slug>/', views.CustomPasswordResetDoneView.as_view(), name='password_reset_done'),
path('password-reset-confirm/<slug:restaurant_slug>/<uidb64>/<token>/', CustomPasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('password-reset-complete/<slug:restaurant_slug>/', auth_views.PasswordResetCompleteView.as_view(template_name='restaurants/password_reset_complete.html'), name='password_reset_complete'),
Мой пароль_сброс_почты.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Reset</title>
</head>
<body style="font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 0;">
<div style="background-color: {{ primary_color }}; color: #ffffff; padding: 20px; text-align: center;">
<h1 style="margin: 0;">{{ restaurant.name }}</h1>
</div>
<div style="padding: 20px;">
<p>Hello,</p>
<p>Debug Info:</p>
<p>restaurant_slug: {{ restaurant_slug }}</p>
<p>uid: {{ uid }}</p>
<p>token: {{ token }}</p>
<p>We received a request to reset your password for your {{ restaurant.name }} account. If you did not make this request, please ignore this email.</p>
<p>To reset your password, click the button below:</p>
<a href="{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' restaurant_slug=restaurant_slug uidb64=uid token=token %}" style="background-color: {{ primary_color }}; color: #ffffff; padding: 10px 20px; text-decoration: none; border-radius: 5px; display: inline-block;">Reset Password</a>
<p>If the button above does not work, paste this link into your web browser:</p>
<p>{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' restaurant_slug=restaurant_slug uidb64=uid token=token %}</p>
<p>Thank you,<br>{{ restaurant.name }} Team</p>
</div>
</body>
</html>
Отладочные сообщения, которые я получаю:
DEBUG: get_context_data - restaurant_slug: dominos
[01/Sep/2024 12:39:18] "GET /password-reset/dominos/ HTTP/1.1" 200 21115
DEBUG: form_valid - restaurant_slug: dominos
DEBUG: form_valid - context: {'uid': 'OA', 'token': 'ccodtk-d092b2b859d47dd4b086be245bd251f6', 'protocol': 'http', 'domain': '127.0.0.1:8000', 'restaurant_slug': 'dominos', 'restaurant': <Restaurant: Dominos>, 'primary_color': '#2445bc'}
DEBUG: form_valid - Full html_content:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Reset</title>
</head>
<body style="font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 0;">
<div style="background-color: #2445bc; color: #ffffff; padding: 20px; text-align: center;">
<h1 style="margin: 0;">Dominos</h1>
</div>
<div style="padding: 20px;">
<p>Hello,</p>
<p>Debug Info:</p>
<p>restaurant_slug: dominos</p>
<p>uid: OA</p>
<p>token: ccodtk-d092b2b859d47dd4b086be245bd251f6</p>
<p>We received a request to reset your password for your Dominos account. If you did not make this request, please ignore this email.</p>
<p>To reset your password, click the button below:</p>
<a href="http://127.0.0.1:8000/password-reset-confirm/dominos/OA/ccodtk-d092b2b859d47dd4b086be245bd251f6/" style="background-color: #2445bc; color: #ffffff; padding: 10px 20px; text-decoration: none; border-radius: 5px; display: inline-block;">Reset Password</a>
<p>If the button above does not work, paste this link into your web browser:</p>
<p>http://127.0.0.1:8000/password-reset-confirm/dominos/OA/ccodtk-d092b2b859d47dd4b086be245bd251f6/</p>
<p>Thank you,<br>Dominos Team</p>
</div>
</body>
</html>
DEBUG: form_valid - html_content: <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=de...
Я также получаю электронное письмо с правильным названием.
Просто я получаю эту ошибку каждый раз, когда пытаюсь отправить письмо для сброса пароля:
Я не могу понять, почему эта ошибка возникает даже после получения правильного slug в моем письме.