2012-04-02 2 views
0

Im django comments app. 게시 페이지가 아닌 게시를 클릭하면 현재 페이지로 리디렉션하는 가장 좋은 방법은 무엇입니까?Django가 게시/미리보기에서 리디렉션했습니다.

나는이 가이드를 따라 한

:

을 :

def comment_posted(request): 
    if request.GET['c']: 
     comment_id, post_id = request.GET['c'].split(':') 
     post = Image.objects.get(pk=post_id) 
     if post: 
      return HttpResponseRedirect(post.get_absolute_url()) 
    return HttpResponseRedirect("/") 

내 urls.py는 다음과 같습니다처럼

{% load comments i18n %} 

<form action="{% comment_form_target %}" method="post">{% csrf_token %} 
    {% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %} 
     {% for field in form %} 
      {% if field.is_hidden %} 
       <div>{{ field }}</div> 
      {% else %} 
       {% if field.name != "email" and field.name != "url" %} 
        {% if field.errors %}{{ field.errors }}{% endif %} 
         <p 
          {% if field.errors %} class="error"{% endif %} 
          {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}> 
          {{ field.label_tag }} {{ field }} 
         </p> 
       {% endif %} 
      {% endif %} 
     {% endfor %} 
    <p class="submit"><input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" /></p> 
</form> 

내 views.py 보이는 : 같은 http://www.omh.cc/blog/2008/mar/9/free-comments-redirection/

내 양식이 보인다

urlpatterns = patterns('', 
    url(r'^other/', include('other.urls')), 
    url(r'^live/', include('live.urls')), 
    url(r'^photo/', include('photo.urls')), 
    url(r'^comments/posted/$', 'photo.views.comment_posted'), 
    url(r'^comments/', include('django.contrib.comments.urls')), 
    url(r'^search/', SearchView(template=None, searchqueryset=None, form_class=SearchForm), name='haystack_search'), 

예고편 :

Environment: 


Request Method: GET 
Request URL: http://127.0.0.1:8000/comments/posted/?c=10 

Django Version: 1.3.1 
Python Version: 2.6.6 
Installed Applications: 
['django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.sites', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'other', 
'live', 
'photo', 
'haystack', 
'django.contrib.flatpages', 
'django.contrib.comments', 
'django.contrib.admin', 
'django.contrib.admindocs'] 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware') 


Traceback: 
File "/export/mailgrp4_a/sc10jbr/lib/python/django/core/handlers/base.py" in get_response 
    111.       response = callback(request, *callback_args, **callback_kwargs) 
File "/home/cserv2_a/soc_ug/sc10jbr/WWWdev/dbe/photo/views.py" in comment_posted 
    17.   comment_id, post_id = request.GET['c'].split(':') 

Exception Type: ValueError at /comments/posted/ 
Exception Value: need more than 1 value to unpack 

내 views.py를 잘못 수정 한 것 같습니다.

내 앱의 이름이 사진이고 내 모델이 이미지라고합니다.

감사합니다.

+0

전체 추적 다시 – jpic

+0

ive 님이 원래 게시물을 업데이트했습니다. 감사합니다 –

+0

당신은 어디에서 얻었습니까? comment_id, post_id = request.GET [ 'c']. split (':') – jpic

답변

1

왜 comment_posted보기가 필요한지 알 수 없습니다. 대신, 나는 당신이 당신의 다음 필드를 수정해야한다고 생각하십시오 "다음"환경 변수가 설정되어있는 경우 여기에

{% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %} 

는 "다음"숨겨진 입력 만 출력 됨된다. 당신은 무엇을 목표로해야하는 것입니다 :

  • 이 될 옆에있는 {{다음}} 주석 객체의 절대 URL에 가능
  • 대체는

그것은처럼 보일 수있는 경우 :

<input type="hidden" name="next" value="{% if next %}{{ next }}{% else %}{{ form.target_object.get_absolute_url }}{% endif %}" /> 

여기에는 모델에 올바르게 정의 된 get_absolute_url 메소드가 있다고 가정합니다. 내가 읽기에 의해 form.target_object에 대해 생각

참고 :

  1. 내가 대상 첫 번째 인수로 객체와

  2. 로 실체화 코멘트 양식을 발견 주석 templatetag에 대한 코드, 발견 한 주석 형식의 코드는 target_object 속성에 전달 된 대상 객체를 저장하여 {{form}}이 어디에 있든 사용할 수 있도록합니다.

+0

그 줄이 바뀌 었습니다. 이제 빈 페이지로 이동합니다. NEXT는 어떻게 설정합니까? –

+0

그리고 뭐라구? 내 업데이트에서는 form.instance.content_object보다는 form.target_object를 선호합니다. – jpic

+0

해결 했습니까? – jpic

관련 문제