admin管理员组文章数量:1393160
I have a UserDetail page that includes several forms.
from allauth.account.forms import ChangePasswordForm, AddEmailForm
User = get_user_model()
class UserDetailView(LoginRequiredMixin, DetailView):
model = User
slug_field = "username"
slug_url_kwarg = "username"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# context["form_info_username"] =
context["form_add_mail"] = AddEmailForm(user=self.request.user)
context["form_change_password"] = ChangePasswordForm(user=self.request.user)
return context
When I add a new email:
<form method="post" action="{% url 'account_email' %}" class="add_email mt-2">
{% csrf_token %}
{{ form_add_mail|crispy }}
<div class="flex justify-end mt-2">
<button type="submit"
name="action_add"
class="bg-green-600 hover:bg-green-800 dark:bg-green-600 dark:hover:bg-green-800 font-semibold text-white rounded-sm p-2 text-xs sm:text-sm sm:px-8 ">
{% translate "Add E-mail" %}
</button>
</div>
</form>
The form/view({% url 'account_email' %}
) redirects to http://localhost:8000/de/accounts/email/, but I would prefer it to redirect to the same page where I submitted instead.
I have a UserDetail page that includes several forms.
from allauth.account.forms import ChangePasswordForm, AddEmailForm
User = get_user_model()
class UserDetailView(LoginRequiredMixin, DetailView):
model = User
slug_field = "username"
slug_url_kwarg = "username"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# context["form_info_username"] =
context["form_add_mail"] = AddEmailForm(user=self.request.user)
context["form_change_password"] = ChangePasswordForm(user=self.request.user)
return context
When I add a new email:
<form method="post" action="{% url 'account_email' %}" class="add_email mt-2">
{% csrf_token %}
{{ form_add_mail|crispy }}
<div class="flex justify-end mt-2">
<button type="submit"
name="action_add"
class="bg-green-600 hover:bg-green-800 dark:bg-green-600 dark:hover:bg-green-800 font-semibold text-white rounded-sm p-2 text-xs sm:text-sm sm:px-8 ">
{% translate "Add E-mail" %}
</button>
</div>
</form>
The form/view({% url 'account_email' %}
) redirects to http://localhost:8000/de/accounts/email/, but I would prefer it to redirect to the same page where I submitted instead.
1 Answer
Reset to default 0I fixed it myself.
I have created a CustomEmailView that inherits from allauth where I have overwritten the success_url.
from allauth.account.views import EmailView
class CustomEmailView(EmailView):
def get_success_url(self):
return reverse('users:detail', kwargs={'username': self.request.user.username})
Then I added this view to my app urls.py
:
app_name = "users"
urlpatterns = [
path('accounts/email/', CustomEmailView.as_view(), name='account_email')]
And changed the url in form to users:account_email
:
<form method="post" action="{% url 'users:account_email' %}"
class="add_email mt-2">
{% csrf_token %}
{{ form_add_mail|crispy }}
<div class="flex justify-end mt-2">
<button type="submit"
name="action_add"
class="bg-green-600 hover:bg-green-800 dark:bg-green-600 dark:hover:bg-green-800 font-semibold text-white rounded-sm p-2 text-xs sm:text-sm sm:px-8 ">
{% translate "Add E-mail" %}
</button>
</div>
</form>
本文标签: pythonHow can I change the success url of allauth AddEmailFormStack Overflow
版权声明:本文标题:python - How can I change the success url of allauth AddEmailForm? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744611382a2615676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论