Django-reverse URL' s

时间:2014-11-10 21:05:55

标签: python django url url-redirection

我正在尝试使用Django表单。我试图创建一个表单,它接受一个城市的名称作为输入和输出坐标作为输出。我的应用程序名称是rango.Im接受输入后在URL反向有很多麻烦形式..

我的项目/ urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
import os

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

      url(r'^admin/', include(admin.site.urls)),
      url(r'^rango/',include('rango.urls', namespace="rango")),
)


if settings.DEBUG:
    urlpatterns += patterns(
        'django.views.static',
        (r'media/(?P<path>.*)',
        'serve',
        {'document_root': settings.MEDIA_ROOT}), )

我的rango / urls.py :( rango是应用程序的名称)

from django.conf.urls import patterns, url
from rango import views

urlpatterns = patterns('',
        url(r'^welcome/$', views.index, name='index'),
        url(r'^about/$', views.about, name='about_page'),
        url(r'^categories/(?P<name_dir>\w+)/$',views.cats,name='cats'),
        url(r'^disp_page/(?P<city>\w+)/$',views.geo,name='coods'),
        url(r'^disp_page/$', views.disp_page, name='disp_page')

我的forms.py:

来自django导入表单 来自rango.models导入页面,类别

class PageForm(forms.ModelForm):
    title = forms.CharField(max_length=128, help_text="Please enter the name of the city.")
    #url = forms.URLField(max_length=200, help_text="Please enter the URL of the page.")
    #views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)

    class Meta:
        # Provide an association between the ModelForm and a model
        model = Page
        exclude = ('category','url','views')

我的views.py:

from django.shortcuts import render
from django.http import HttpResponse

from django.template import RequestContext
from django.shortcuts import render_to_response
from rango.models import Category,Page
from pygeocoder import Geocoder
from rango.forms import PageForm
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

import operator
# Create your views here.




def geo(request,city):
    context = RequestContext(request)

    citydata=Geocoder.geocode(city)
    codtuple=citydata[0].coordinates
    codtuple=list(codtuple)
    context_dict = {'cood':codtuple}

    return render_to_response('coods.html',context_dict,context)



def disp_page(request):
    # A HTTP POST?
    if request.method == 'POST':
        form = PageForm(request.POST)

        # Have we been provided with a valid form?
        if form.is_valid():
            # Save the new category to the database.
            #form.save(commit=True)
            city = form.cleaned_data['title']


           # context = RequestContext(request)

            #citydata=Geocoder.geocode(cityname)
            #codtuple=citydata[0].coordinates
            #codtuple=list(codtuple)
            #context_dict = {'cood':codtuple}


            return HttpResponseRedirect(reverse('rango:geo', args=(request,city)))


        else:
            # The supplied form contained errors - just print them to the terminal.
            print form.errors
    else:
        # If the request was not a POST, display the form to enter details.
        form = PageForm()

    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    return render(request, 'disp_page.html', {'form': form})

基本上disp_page显示表单。我在表单中键入城市的名称(EX:NEWYORK),并且必须在我的views.py中重定向到“geo”函数,这将输出一个坐标不同的观点。这种重定向似乎没有发生 - 任何帮助都表示赞赏!!

1 个答案:

答案 0 :(得分:3)

更改此行

url(r'^disp_page/(?P<city>\w+)/$',views.geo,name='coods'),

rango/urls.py中:

url(r'^disp_page/(?P<city>\w+)/$',views.geo,name='geo'),

并使用:

return HttpResponseRedirect(reverse('rango:geo', args=(city,)))