null_pointer028
null_pointer028

Reputation: 21

django - invalid syntax regex

I'm trying to implement the solution to error

ImproperlyConfigured
TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()

Here's my urls.py as suggested

from django.urls import include, path, re_path

urlpatterns = [
    re_path(r'^auth/registration/account-confirm-email/(?P<key>[\s\d\w().+-_',:&]+)/$', TemplateView.as_view(), name = 'account_confirm_email'),
    path('auth/registration/', include('dj_rest_auth.registration.urls')),
]

But I'm getting this error.

re_path(r'^registration/account-confirm-email/(?P<key>[\s\d\w().+-_',:&]+)/$', TemplateView.as_view(), name = 'account_confirm_email'),
                                                                     ^
SyntaxError: invalid syntax

Any idea on what I could be missing? Thanks!

Upvotes: 2

Views: 61

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

The syntax highlighting already shows the problem, there is a quite in your regex that ends the regex prematurely. You can fix this using double quotes:

from django.urls import include, path, re_path

urlpatterns = [
    re_path(
        r"^registration/account-confirm-email/(?P<key>[\s\d\w().+-_',: & ]+)/$",
        TemplateView.as_view(),
        name='account_confirm_email'
    ),
    path('auth/registration/', include('dj_rest_auth.registration.urls')),
]

But this will not fix the first error. This is complaining about the fact that you did not pass a template_name parameter to the TemplateView:

urlpatterns = [
    re_path(
        r"^registration/account-confirm-email/(?P<key>[\s\d\w().+-_',: & ]+)/$",
        TemplateView.as_view(template_name='some_template.html'),
        name='account_confirm_email'
    ),
    path('auth/registration/', include('dj_rest_auth.registration.urls')),
]

As @HakenLid says, perhaps you can also simplify the regex to (?P<key>.+), this of course alters the semantics a bit.

Upvotes: 3

Related Questions