python - Django URL Routing misbehaving, routing to an unanticipated path -
i made changes model.py, foreignkey adjustments. (i realized placed them on wrong class). deleted database , created new scratch since far in. when felt corrected in db run, , ran runserver again, keyerror error, path shouldn't directed to.
when type in url 127.0.0.1:8000/add_tech/, or add_exp/ loads right form, when submit, shows function received term tech or exp through regex in urls.py, disappointingly stack trace output shows differently.
the views.py doesn't seem routing correctly. there steps should follow step through , debug this?
urls.py has following
from django.conf.urls import patterns, include, url resume.views import add_entry, remove_entry urlpatterns = patterns('', url(r'^add_(\w+)/$',add_entry), url(r'^remove_(\w+)/$',remove_entry), )
it's being routed views.py in resume/: (as seen)
def add_entry(request, option): print "print option in url is:"+option #<----- see console out below. options={ 'job':add_job(request), 'exp':add_exp(request), 'tech':add_tech(request), 'course':add_course(request), 'project':add_project(request) } form_html = options[option] return form_html
this print displays in console, should routed add_exp, not sure how it's being routed add_tech, see when error raised.:
[18/may/2014 17:01:21] "get /add_exp/ http/1.1" 200 1446 print option in url is:**exp** internal server error: **/add_exp/** traceback (most recent call last): file "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 115, in get_response response = callback(request, *callback_args, \*\*callback_kwargs) file "/home/asilvajr/repository/mysite/resume/views.py", line 120, in **add_entry** 'tech':add_tech(request), file "/home/asilvajr/repository/mysite/resume/views.py", line 19, in **add_tech** return submit_tech(request) file "/home/asilvajr/repository/mysite/resume/views.py", line 76, in **submit_tech** if vals['tech_name']: keyerror: 'tech_name' ---- end console output -----
just form, in tech_form (should loaded before , after submit):
<form action="/add_tech/" method="post"> <label for="tech">technolgy used:</label> <input type="text" name="tech_name"><br/> <label for="type">type:</label> <select name="tech_type"> <option name="l">language</option> <option name="fr">framework</option> <option name="t">tools</option> <option name="k">kits</option> <option name="e">extensions</option> </select><br/> <input type="submit" name="submit"><br/> </form>
just form, in exp_form (should loaded before , after submit):
<form action="/add_exp/" method="post"> <label for="event">event:</label> <input type="text" name="event"><br/> <label for="description">description:</label> <textarea type="textarea" name="exp_descript"></textarea><br/> <label for="technology">technology used:</label> {% tech in techs %} <input type="checkbox" class="tech_{{ tech.tech_type }}" name="technologies" value="{{ tech.id }}">{{ tech.name }} :: {{ tech.tech_type }}<br/> {% endfor %} <input type="submit" name="submit"><br/> </form>
both add_tech , add_exp designed so:
def add_tech(request): if 'submit' in request.post: return submit_tech(request) techs = technologies.objects.all() return render(request, 'tech_form.html',{'techs':techs})
submit_tech requested:
def submit_tech(request): vals = request.post.dict() vals['errors']=[] if vals['tech_name']: try: exists=technologies.objects.get(name=vals['tech_name']) except technologies.doesnotexist: exists=none if not exists: tech = technologies(name=vals['tech_name'],tech_type=vals['tech_type']) tech.save() else: vals['errors'].append(exists.name + "already exists " + exists.tech_type) else: vals['errors'].append("added nothing, text feild empty") vals['techs'] = technologies.objects.all() return render(request, 'tech_form.html', vals)
i created test project , copy paste codes. works fine here, can post /add_tech/
, , new technologies
created in db.
i suggest check render()
statement in add_exp
function, template name correct?
additionally, 2 suggestions: 1). in add_entry
funciton, add handling invalid entry name; 2). in 3rd line of submit_tech
function, use if vals.get('tech_name', none):
, otherwise codes raise 500 error when submit invalid form
Comments
Post a Comment