2

我在 pythonanywhere 工作并且我已经运行了一个注册脚本 - 本地它在 pythonanywhere 中没有问题我得到这个错误

2020-09-13 15:37:43,507: Internal Server Error: /signup1/
Traceback (most recent call last):
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/username/werkvertragrepo/werkvertrag/views.py", line 612, in signup1
text_body = render_to_string('acc_active_email.txt', merge_data)
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/template/base.py", line 170, in render
return self._render(context)
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/template/defaulttags.py", line 446, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/urls/base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'activate' with keyword arguments 
'{'uidb64': 'NTA', 'token': 'aa5a2v-c248dcaea908540be1e8e3d5feff970f1'}' not found. 1 
pattern(s) tried: ['activate/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A- 
Za-z]{1,20})/']

服务器日志

2020-09-13 15:37:43 {'user': <User: user@usermail.de>, 'domain': 'www.mywebsite.de', 
'uid': 'NTA', 'token': 'aa5a2v-c248dcaea908540be1e8e3d5feff970f1'}
2020-09-13 15:37:43 Internal Server Error: /signup1/#012Traceback (most recent call l 
ast):#012  File "/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/core/handlers/exception.py", line 47, in inner#012    response = 
get_response(request)#012  File 
"/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/core/handlers/base.py", line 179, in _get_response#012    response = 
wrapped_callback(request, *callback_args, **callback_kwargs)#012  File 
"/home/username/werkvertragrepo/werkvertrag/views.py", line 612, in signup1#012    text_body 
= render_to_string('acc_active_email.txt', merge_data)#012  File 
"/home/username/.virtualenvs/myenv/lib/python3.6/site-packages/django/template/loader.py", 
line 62, in render_to_string#012    return template.render(context, request)#012  File 
"/home/username/.virtualenvs/myenv/lib/python3.6/site- 
packages/django/template/backends/django.py", line 61, in render#012    return self.template

如果发现错误出现在此行的视图中:

text_body = render_to_string('acc_active_email.txt', merge_data)

在我的本地主机上它可以正常工作

视图.py

def signup1(request):
    if request.method == 'POST':
        form = werkforms.UserCreationform(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            email = form.cleaned_data.get('email')
            raw_password = form.cleaned_data.get('password1')
            Firmennamen = form.cleaned_data.get('Firmennamen')
            Street = form.cleaned_data.get('Street')
            plzort = form.cleaned_data.get('plzort')

            user = User.objects.create_user(username, email, raw_password)
            user.is_active = False
            user.save()
            userid = user.id
            UserID = User.objects.get(id=userid)
            current_site = get_current_site(request)
            print("hallo")


            merge_data = {
                'user': user,
                'domain': current_site.domain,
                'uid': force_text(urlsafe_base64_encode(force_bytes(user.pk))),
                'token': account_activation_token.make_token(user),
            }
            print(merge_data) 
            text_body = render_to_string('acc_active_email.txt', merge_data)

            --- some more code --- 

            test = "das ist response"

    return HttpResponse(test, content_type="text/html")

acc_active_email.txt

Hallo {{ user }}, 

Willkomen beim Onlineforum,

Deine Registrierung war erfolgreich. Klicke nun auf den unteren Link um deine Registrierung abzuschließen.

http://{{ domain }}{% url 'activate' uidb64=uid token=token %}


Vielen Dank

Mit freundlichen Grüßen
4

2 回答 2

1

在您必须设置的网址中

path('activate/<slug:uidb64>/<slug:token>/',
         activate, name='activate'),

并且不要忘记从视图激活中导入网址

from myapp.views import (
    activate
    )
于 2020-09-13T16:50:24.023 回答
0

您还应该在激活函数中使用上下文参数:

context = {'uidb64': uidb64, 'token': token} 
return render(request, 'users/thank_confirm.html', context)

有人建议在 uid 键中使用 decode() func,但我认为应该只是:

'uid': urlsafe_base64_encode(force_bytes(user.pk))

主要问题在于您的应用程序的网址(包括激活视图)。你应该使用蛞蝓:

path('activate/<slug:uidb64>/<slug:token>/', views.activate, name='activate')

如果您只使用 url,请不要忘记导入路径

from django.urls import path
于 2020-10-20T12:01:26.983 回答