NoReverseMatch在/ accounts / home_page /

时间:2017-01-06 17:43:16

标签: python django

#Reverse for 'user_profile_view' with arguments '(u'Emmanuel',)' and keyword arguments '{}' not found. 0 pattern(s) tried: [] :

#project/urls.py:
        from django.conf.urls import url, include
        from django.contrib import admin
        from django.conf import settings
        from django.conf.urls.static import static

        urlpatterns = [
              url(r'^admin/', admin.site.urls),
              url(r'^accounts/', include('registration.backends.default.urls')),
              url(r'', include('app.urls', namespace='app')),
          ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)



 #app/urls.py

        from .views import *
        from . import views
        from django.conf import settings
        from django.conf.urls import url
        from django.views.generic import TemplateView
        from django.conf.urls.static import static

        app_name = 'app'
        urlpatterns = [
              url(r'^$', views.IndexView.as_view(), name='index'),
              url(r'^accounts/home_page/$', home_page),
              url(r'^accounts/home_page/(?P<username>[\w-]+)/$', UserProfileView.as_view(), name='user_profile_view'),
                      # url(r'^accounts/profile/$', views.user_profile, name='user_profile'),
              # url(r'^accounts/profile/edit/$', views.edit_user_profile,
              #    name='edit_user_profile'),
                  ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


#views.py

        class UserProfileView(View):
        @method_decorator(login_required)
        def get(self, request, user):
            if request.user.username == user:
                profile = get_object_or_404(User, user=request.user)
                return render(request, 'registration/home.html', {'profile': profile})
            else:
                raise Http404


          @login_required
          def home_page(request):
              return HttpResponseRedirect(
                  reverse('user_profile_view', args=[request.user.username],    current_app='app'))

所以我实际上要做的是允许用户登录,其名称出现在网址中,但由于LOGIN_REDIRECT_URL ='/ accounts / home_page'不能采用动态参数,例如我正在使用重定向。但我得到这个错误,请问我做错了什么。 在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

#views.py    
class UserProfileView(View):
        @method_decorator(login_required)
        def get(self, request, username):
            if request.user.username == username:
                profile = get_object_or_404(UserExtended, user=request.user)
                return render(request, 'registration/home.html', {'profile': profile})
            else:
                raise Http404


    @login_required
    def home_page(request):
        return HttpResponseRedirect(
            reverse('app:user_profile_view',
                    args=[request.user.username],  current_app='app'))

所以我改变了什么时候跟着什么@ShanWang说我得到了错误:

TypeError异常值:get()得到一个意外的关键字参数'username' 因为我的def get(self, request, username):def get(self, request, user):所以我改变了这一点

if request.user.username == user: 

 if request.user.username == username:

这就是诀窍