DjangReverse URL的

2024-10-01 17:31:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Django表单。我尝试创建一个表单,它将一个城市的名称作为输入,输出坐标为输出。我的应用程序名称为兰戈。Im在接受表单的输入后,URL反向有很多问题。。在

我的项目/网址.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}), )

我的兰戈/网址.py:(rango是应用程序的名称)

^{pr2}$

我的表单.py公司名称:

从django导入表单 从兰戈模型导入页面,类别

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')

我的视图.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_页面显示表单,我在表单中输入一个城市的名称(例如:NEWYORK)以及它必须重定向到my中的“geo”函数视图.py它将以不同的视图。这个重定向似乎没有发生任何帮助是感谢!!在


Tags: thedjangofrompyimportform名称url

热门问题