我想使用同一视图通过他的django表单显示或更新对象

时间:2018-07-16 07:59:13

标签: python django django-forms

有人可以帮助我解决这个问题吗?

models.py

@autoconnect
class Event(models.Model):
  title = models.CharField(max_length=50, unique=True)
  slug = models.CharField(max_length=100, default=' ', blank=True)
  description = models.CharField(max_length=250, blank=True)
  PrivacityType = ((1, 'Público'), (2, 'Privado'))
  privacity = models.IntegerField(choices=PrivacityType, default=1, blank=True)
  EVENT_TYPE = ((1, 'Deporte'), (2, 'Nutrición'), (3, 'Salud'))
  type = models.IntegerField(choices=EVENT_TYPE, default=1)
  owner = models.ForeignKey(User)
  creation_date = models.DateTimeField(auto_now=True)
  updated_date= models.DateTimeField(auto_now_add=True)
  start = models.DateTimeField(default=datetime.datetime.now())
  end = models.DateTimeField(default=datetime.datetime.now())
  address = models.CharField(max_length=250, default='', blank=True)
  notes = models.CharField(max_length=250, default='', blank=True)

  def __unicode__(self):
    return self.title

  def pre_save(self):
    self.slug = self.title.replace(" ", "_").lower()

forms.py

class EventForm(forms.ModelForm):
  title = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Título'}))
  description = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Descripción'}))
  address = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Lugar'}))
  notes =forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Añade tus notas'}))

  class Meta:
    model = event_models.Event
    fields = '__all__'

views.py

class EventUpdateView(LoginRequiredMixin, UpdateView):
  model = events_models.Event
  template_name = 'event.html'
  fields = ['title', 'description']
  slug_field = 'slug'
  slug_url_kwarg = 'slug'
  context_object_name = 'event'

urls.py

url(r'^eventos/(?P<slug>\w+)/$', calendar_views.EventUpdateView.as_view(), name='detail_event'),
url(r'^eventos/nuevo/$', calendar_views.add_event, name='add_event'),

event.html:

{% extends "calendar.html" %}
{% load static %}
{% load calendar_tags %}

{% block event %}

<!-- Event Information Area -->
  <div class="col-md-6 col-lg-6 col-sm-12 col-xs-12 smt-30 xmt-30">
    <div class="map-contacts">
      <!--<form id="contact-form" method="POST">-->
      <form action="{% url 'calendar:detail_event' event_slug=event.slug %}" method="POST">
        {% csrf_token %}

        <!-- Event Form Area -->
        <div class="single-contact-form">
            <div class="contact-box subject">
              <h3>Privacidad</h3>
              {{ event.privacity }}
            </div>
        </div>
        <!-- Event Form Area -->
        <div class="single-contact-form">
            <div class="contact-box subject">
              <h3>Categoría</h3>
              {{ event.type }}
            </div>
        </div>
        <!-- Event Form Area -->
        <div class="single-contact-form">
            <div class="contact-box subject">
              {{ event.title }}
            </div>
        </div>
        <!-- Event Form Area -->
        <div class="single-contact-form">
            <div class="contact-box subject">
              {{ event.description }}
            </div>
        </div>
        <!-- Event Form Area -->
        <div class="single-contact-form">
            <div class="contact-box subject">
              {{ event.address }}
            </div>
        </div>
        <!-- Event Form Area -->
        <div class="single-contact-form">
            <div class="contact-box subject">
              <h3>Inicio</h3>
              {{ event.start }}
            </div>
        </div>
        <!-- Event Form Area -->
        <div class="single-contact-form">
            <div class="contact-box subject">
              <h3>Fin</h3>
              {{ event.end }}
            </div>
        </div>
        <!-- Event Form Area -->
        <div class="single-contact-form">
            <div class="contact-box subject">
              <h3>Privacidad</h3>
              {{ event.notes }}
            </div>
        </div>

        <div class="contact-btn">
            <input type="submit" class="fv-btn" value="Modificar">
        </div>
      </form>
    </div>
  </div>

{% endblock %}

模板调用:

<a href="{% url 'calendar:detail_event' event.slug %}">{{ event.title }}</a>

这是出现的错误。 我该如何解决?

未找到带有关键字参数'{u'event_slug':u'evento_1'}'的'detail_event'。尝试了1种模式:[u'calendario / eventos /(?P \ w +)/ $']

谢谢。

1 个答案:

答案 0 :(得分:0)

您的方法无效,因为您需要输入event_slug才能创建活动的表单。但是event_slug直到 创建事件后才存在!

通常的解决方案是具有单独的“创建”和“更新”视图。 为此,请检查generic editing views。例如,您可以导航到/event/new/以创建一个新事件(没有任何提示),然后在该提示存在后转到/event/(?P<slug>\w+)/编辑该事件。

This answer也应该帮助您。