0

Someone could help me with this problem ?

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 %}

The template call:

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

This is the error that appears. How can i fix it ?

Reverse for 'detail_event' with keyword arguments '{u'event_slug': u'evento_1'}' not found. 1 pattern(s) tried: [u'calendario/eventos/(?P\w+)/$']

Thanks in advance.

1 Answers1

0

Your approach won't work because you need an event_slug to reach the form where you will create the event. But the event_slug won't exist until after you create the event!

The usual solution is to have separate 'create' and 'update' views. Check out the generic editing views for this purpose. For example, you could navigate to /event/new/ to create a new event (without slug) and then go to /event/(?P<slug>\w+)/ to edit that event once the slug exists.

This answer should also help you out.

gatlanticus
  • 1,158
  • 2
  • 14
  • 28
  • I'm going to do it in separate views and i will write if I have managed to fix it – Alberto Sanmartin Martinez Jul 16 '18 at 13:23
  • I'am revising the code and i want something like this: [https://stackoverflow.com/a/7493320/8000016]. My idea is that if the event exists, initialize the form with the information of the object and start it empty if not exist. – Alberto Sanmartin Martinez Jul 16 '18 at 14:23
  • Django's [UpdateView](https://docs.djangoproject.com/en/2.0/ref/class-based-views/generic-editing/#updateview) does that automatically (see the example, where you simply choose which fields to render). You just need to use `/events/(?P\w+)/` (i.e. with `slug` as the keyword not `event_slug`, so that Django automatically knows which object to load. So ultimately, use the UpdateView with the url path above for the edit view, and use a CreateView with `/events/new/` or similar (but no slug). – gatlanticus Jul 16 '18 at 21:04
  • @AlbertoSanmartinMartinez Ok, please upvote my solution if it has been helpful, and also mark it as correct if it solves your problem – gatlanticus Jul 17 '18 at 06:54
  • Thanks !! I upvoted your comment, but now appears other error, i posted the modifications in the general question and actualize the error message. – Alberto Sanmartin Martinez Jul 17 '18 at 08:55
  • Where is the template call? I don't see it in event.html... You could also make life easier for yourself by deleting the action variable from the form tag: it will just post to the same page by default, without you having to make it work – gatlanticus Jul 17 '18 at 11:19
  • The template call for EventUpdateView is in day_calendar.html and month_calendar.html, in each event detail view link. I deleted the action tag form but now the event update view doesn't appear like form, only the information. – Alberto Sanmartin Martinez Jul 17 '18 at 11:34