无法从表单发布数据

时间:2016-01-02 17:00:05

标签: django django-views

我是django的新手,我想从表单发布数据,但它不起作用。我不知道我需要发布什么。所以我发布了每个代码。

这是post_page.html

<form action="" method="POST">
        {% csrf_token %}
        <div class="tab-content">
            <div class="tab-pane active" role="tabpanel" id="step1">
                <select class="image-picker show-labels show-html" name="category">
                    <option data-img-src="{% static 'img/type_move.png' %}" value="img/type_move.png">搬運</option>
                    <option data-img-src="{% static 'img/type_clean.png' %}" value="img/type_clean.png">清掃</option>
                    <option data-img-src="{% static 'img/type_recept.png' %}" value="img/type_recept.png">接待</option>
                    <option data-img-src="{% static 'img/type_guide.png' %}" value="img/type_guide.png">引導</option>
                    <option data-img-src="{% static 'img/type_camera.png' %}" value="img/type_camera.png">攝像</option>
                    <option data-img-src="{% static 'img/type_leader.png' %}" value="img/type_leader.png">領隊</option>
                    <option data-img-src="{% static 'img/type_trans.png' %}" value="img/type_trans.png">翻譯</option>
                    <option data-img-src="{% static 'img/type_others.png' %}" value="img/type_others.png">其他</option>
                </select>
            </div>
<div class="tab-pane" role="tabpanel" id="step2">
                <div class="wrap">
                    <div class="input-2">
                        <div>
                            <label for="need-topic">
                                <span class="fa fa-plus"></span> <strong>topic</strong>
                            </label>
                            <input id="need-topic" type="text" name="topic"/>
                        </div>
                        <div>
                            <label for="need-number">
                                <span class="fa fa-child"></span> <strong>total</strong>
                            </label>
                            <input id="need-number" type="text" name="total"/>
                        </div>
                    </div>
                    <div class="input-3">
                        <div>
                            <label for="need-date">
                                <span class="fa fa-calendar"> <strong>date</strong>
                      </label>
                      <input class="datepicker" id="need-date" type="text" name="e_date"/>
                    </div>
                    <div>
                      <label for="need-start">
                        <span class="fa fa-clock-o"> <strong>start_time</strong>
                      </label>
                      <input class="timepicker" id="need-start" type="text" name="start_time"/>
                    </div>
                    <div>
                      <label for="need-end">
                        <span class="fa fa-clock-o"> <strong>end_time</strong>
                      </label>
                      <input class="timepicker" id="need-end" type="text" name="end_time"/>
                    </div>
                  </div>
                  <div class="input-1">
                    <div>
                      <label for="need-location">
                        <span class="fa fa-map-marker"></span> <strong>loaction</strong>
                            </label>
                            <input id="need-location" type="text" name="location"/>
                        </div>
                    </div>
                </div>
                <ul class="list-inline pull-right">
                    <li>
                        <button type="button" class="btn btn-default prev-step">prev-step</button>
                    </li>
                    <li>
                        <button type="button" id="step2-3" class="btn btn-primary next-step">save & next</button>
                    </li>
                </ul>
            </div>
                <ul class="list-inline pull-right">
                    <li>
                        <button type="submit" class="btn btn-default next-step">skip & submit</button>
                    </li>
                    <li>
                        <button type="submit" id="step4-finish" class="btn btn-success btn-info-full next-step">submit <i class="fa fa-exclamation"></i></button>
                    </li>
                </ul>
            </div>
        </div>
    </form>

这里是model.py

from django.db import models
from django.utils import timezone
class Event(models.Model):
e_id = models.AutoField(primary_key=True)

usr_id =  models.CharField(max_length=200)
category = models.CharField(max_length=200)
topic = models.CharField(max_length=200)
total = models.IntegerField()
e_date = models.DateTimeField(default=timezone.now)
start_time = models.TimeField(default=timezone.now)
end_time = models.TimeField(default=timezone.now)
location = models.CharField(max_length=200)

finish = models.BooleanField()
posted_date = models.DateTimeField(default=timezone.now)

这里是views.py

from django.shortcuts import render
from getyou.models import Usr, Event, Event_comment, Event_member
from django import forms
from .forms import EventForm
def create(request):
if request.method == 'POST':
    local_form = EventForm(request.POST)
    if local_form.is_valid():
        print('post')
        my_category = local_form.data.get('category')
        my_topic = local_form.data.get('topic')
        my_total = local_form.data.get('total')
        my_e_date =local_form.data.get('e_date')
        my_start_time = local_form.data.get('start_time')
        my_end_time = local_form.data.get('end_time')
        my_location = local_form.data.get('location')



        Event.objects.create(
            usr_id = 0,
            category = my_category,
            topic = my_topic,
            total= my_total,
            e_date = my_e_date,
            start_time = my_start_time, 
            end_time = my_end_time,
            location = my_location,
            finish = False,
        )

        return render(request, 'index.html')

这里是urls.py

from django.conf.urls import include, url
from django.contrib import admin
from getyou import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.welcome, name='welcome'),
url(r'^index/$', views.index, name='index'),
url(r'^profile/$', views.profile, name='profile'),
url(r'^$', views.create),
]

1 个答案:

答案 0 :(得分:1)

请再次检查urls.py

from django.conf.urls import include, url
from django.contrib import admin
from getyou import views

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.welcome, name='welcome'),
    url(r'^index/$', views.index, name='index'),
    url(r'^profile/$', views.profile, name='profile'),
    url(r'^$', views.create),
]

第二个url(r'^$', views.welcome, name='welcome'),会捕获请求,因此第五个网址无法捕获匹配'^$'模式的网址