2012-12-15 81 views
0

我正在嘗試將視圖重定向到另一個視圖,以便用戶在未滿足條件的情況下無法訪問它。在這種情況下,密碼更改成功。我也想用HttpResponseRedirect來實現這一點。Django - 爲什麼不反轉工作?

現在,我從來沒有使用過反轉,但我認爲這是做到這一點的方法。問題是我無法逆轉工作。我收到以下錯誤:

ViewDoesNotExist at/account/password/ 無法導入findadownload.main.views.foo。 View在模塊findadownload.main.views中不存在。

# URLS 

from django.conf.urls import patterns, include, url 
from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
from django.conf import settings 


#SANDY 
from findadownload.sandy.views import * 


#TRIVIARIA 
from findadownload.trivia.views import * 
from findadownload.accounts.views import * 


# Uncomment the next two lines to enable the admin: 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    # Examples: 
    url(r'^$', 'findadownload.main.views.presidium_home', name='presidium_home'), 
    (r'^web/$', 'findadownload.main.views.web'), 
    (r'^web/developers/$', sandy_developers), 


    #SANDY (for findadownload!) 
    (r'^get/sandy/$', get_sandy), 
    (r'^get/sandy/download/$', get_sandy_download), 
    (r'^sandy/register/$', sandy_registration), 
    (r'^sandy/complete/$', sandy_complete), 
    (r'^web/shared/$', sandy_shared_files), 
    (r'^web/remove/all/$', sandy_remove_all), 
    (r'^web/remove/(?P<file_id>\w+)/$', sandy_remove_file), 
    (r'^about/$', 'findadownload.main.views.about'), 
    (r'^pytech/$', 'findadownload.main.views.pytech'), 
    (r'^pytech/aboutus$', 'findadownload.main.views.pytech_about'), 
    (r'^pytech/services$', 'findadownload.main.views.pytech_services'), 
    (r'^pytech/support$', 'findadownload.main.views.pytech_support'), 
    (r'^pytech/contactus$', 'findadownload.main.views.pytech_contact'), 
    (r'^pytech/sitemap$', 'findadownload.main.views.pytech_sitemap'), 
    (r'^pytech/search/$', 'findadownload.main.views.pytech_search'), 
    (r'^presidium', 'findadownload.main.views.presidium_home'), 


    #TRIVIARIA 
    (r'^register/$', register), 
    (r'^login/$', login_view, {'template': 'trivia/registration/login.html'}), 
    (r'^logout/$', logout_view), 
    (r'^trivia/$', submit_question, {'template': 'trivia/trivia_questions.html'}), 
    (r'^challenges/$', submit_question, {'template': 'trivia/trivia_challenges.html'}), 
    (r'^question/thanks/$', question_thanks), 
    (r'^question/answer/$', submit_answer), 
    (r'^question/answer/result/(\d+)/(.*)/$', answer_result), 
    (r'^question/answer/challenge/$', challenge_answer), 
    (r'^account/$', upload_avatar), 
    (r'^account/password/$', password_change), 
    url(r'^account/password/success/$', 'findadownload.accounts.views.password_success'), 
    (r'^(?P<username>\w+)/$', view_user_profile, {'template': 'trivia/trivia_view_user_questions.html'}), 
    (r'^(?P<username>\w+)/trivia/$', view_user_profile, {'template': 'trivia/trivia_view_user_questions.html'}), 
    (r'^(?P<username>\w+)/challenges/$', view_user_profile, {'template': 'trivia/trivia_view_user_challenges.html'}), 




    #FILE SHARING 
    (r'^web/share/(?P<file_id>\w+)/$', sandy_download), 
    (r'^web/report/(?P<file_id>\w+)/$', sandy_file_report), 
    (r'^web/like/(?P<file_id>\w+)/$', like_download), 
    #OTHER 
    (r'^web/profile/$', 'findadownload.main.views.web_profile'), 
    (r'^web/edit/$', 'findadownload.main.views.web_edit'), 
    (r'^trivia/$', 'findadownload.main.views.trivia_home', {'template': 'trivia/registration/index.html'}), 
    (r'^web/get/$', 'findadownload.main.views.web_get'), 
    (r'^web/progress/$', 'findadownload.main.views.web_progress'), 
    (r'^web/foo/$', 'findadownload.main.views.foo'), 
    (r'^web/search/$', 'findadownload.main.views.web_search'), 
    (r'^web/logout/$', 'findadownload.main.views.web_logout'), 
    (r'^web/resend/$', 'findadownload.main.views.web_resend'), 
    (r'^web/home/$', 'findadownload.main.views.web_home'), 
    (r'^web/history/$', 'findadownload.main.views.web_history'), 
    (r'^web/verify/(?P<link>\w+)/$', 'findadownload.main.views.web_activate'), 
    (r'^web/help/(?P<username>\w+)/$', 'findadownload.main.views.web_help'), 
    (r'^web/welcome/(?P<username>\w+)/$', 'findadownload.main.views.welcome'), 
    (r'^web/terms/$', 'findadownload.main.views.web_terms'), 
    (r'^web/privacy/$', 'findadownload.main.views.web_privacy'), 
    (r'^web/signup/$', 'findadownload.main.views.web_signup'), 
    (r'^web/login/$', 'findadownload.main.views.web_login'), 
    (r'^test$', 'findadownload.main.views.test'), 
    # url(r'^findadownload/', include('findadownload.foo.urls')), 


    # Uncomment the admin/doc line below to enable admin documentation: 
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 

    # Uncomment the next line to enable the admin: 
    url(r'^admin/', include(admin.site.urls)), 
) 


if 1: 
    urlpatterns += patterns('', 
     url(r'^uploads/(?P<path>.*)$', 'django.views.static.serve', { 
      'document_root': settings.MEDIA_ROOT, 
      'show_indexes': True, 
     }), 
    ) 




urlpatterns += staticfiles_urlpatterns() 



    # VIEWS 

    from django.http import HttpResponse, HttpResponseRedirect 
    from django.shortcuts import render_to_response 
    from findadownload.accounts.forms import * 
    from django.contrib.auth.models import User 
    from django.contrib import auth 
    from findadownload.accounts.models import * 
    from django.core.urlresolvers import reverse 
    from findadownload.trivia.models import * 


    def password_success(request): 
     return render_to_response('trivia/trivia_password_success.html') 

    def password_change(request): 
     if request.user.is_authenticated(): 
      # if for is submitted 
      if request.method == 'POST': 
       form = PasswordForm(user=request.user, data=request.POST) 
       # for the avatar part 
       if form.is_valid(): 
        cd = form.cleaned_data 
        request.user.set_password(cd['confirm_password']) 
        request.user.save()`enter code here` 
        # redirect to profile page after POST 
        return HttpResponseRedirect(reverse('findadownload.accounts.views.password_success')) 
      else: 
       form = PasswordForm(user=request.user) 

      # render empty form 
      return render_to_response('trivia/trivia_password_change.html', {'form': form}, 
             context_instance=RequestContext(request)) 
     else: 
      return HttpResponseRedirect('/login/?next=%s' % request.path) 

回答

0

你必須在你的URL指定name kwargs,在這種情況下:

url(r'^account/password/success/$', 'findadownload.accounts.views.password_success', name = 'password_success') 

然後使用逆轉這樣的:

return HttpResponseRedirect(reverse('password_success')) 

reverse通過什麼name kwarg在urlpatterns上。

編輯:

我可以看到你urls.py的其餘部分。我認爲問題不在reverse上,因爲Django無法找到名爲foo的視圖。

+0

k我編輯了整個網址 –

+0

好吧,很酷。我解決了它!那麼,你做到了。問題是我的兄弟正在使用與我使用的urls.py相同的應用程序,並且foo視圖與我的視圖中的反向函數衝突。所以我只是評論它,瞧!它現在有效!我將在後面更詳細地研究這種觀點,看看它是如何引發問題的,但現在我將它留下評論。無論如何,我真的很感謝你的幫助。謝謝 :) –