Python_Django - 添加出席事件选项 - 如何?

时间:2017-05-13 15:39:54

标签: python django django-models django-forms django-views

我的网络应用程序中有3个模型。事件(自制),用户(从django.contrib.auth.models导入)和EventAttendance(自制)。 EventAttendance是一个连接Event和User表的表(通过外键person_key和event_key)。

我想创建一个按钮,用于连接用户(登录的用户)和Event。它应该在表EventAttendance中插入一行。 我试图让它工作,但不幸的是它没有做任何事情。现在我只能通过ADMIN选项卡将数据放入EventAttendance表。

有人可以查看我的代码并给我一些建议吗?

模型

class EventForm(forms.ModelForm):
    class Meta:
    model = Event
    fields = ('title', 'text','place','start','price',)

class EventAttendanceForm(forms.ModelForm):
    class Meta:
    model = EventAttendance
    widgets = {
        'event_key': forms.HiddenInput(),
    }
    fields = ['person_key', 'event_key', 'attended',]

class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)
    class Meta:
    model = User
    fields = ( "username", "email" )

FORMS `

def event_attendance(request,pk):
    event = get_object_or_404(Event, pk=pk)
    #if request.method == "POST":
    form = EventAttendanceForm(request.POST)
    EventAttendance = form.save(commit=False)
    EventAttendance.person_key = user.id
    EventAttendance.event_key = event.id
    EventAttendance.attended = 1
    EventAttendance.save()
    return redirect('event_detail', pk=event.pk)

    return render(request, 'events/post_detail.html', )

 def event_new(request):
     if request.method == "POST":
        form = EventForm(request.POST)
        if form.is_valid():
           event = form.save(commit=False)
           event.author = request.user
           event.published_date = timezone.now()
           event.save()
           return redirect('event_detail', pk=event.pk)
     else:
          form = EventForm()
     return render(request, 'events/post_edit.html', {'form': form})

查看

{% extends 'events/base.html' %}

{% block content %}
  <div class="event">
    {% if event.published_date %}
        <div class="date">
          <p> <u>Pridane:</u> {{ event.published_date }} </p>
          <p> </p>
          <p> <u>Autor:</u> {{ event.author }} </p>

          <br>
        </div>
    {% endif %}
    <a class="btn btn-default" href="{% url 'event_edit' pk=event.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
    <a class="btn btn-default" href="{% url 'event_remove' pk=event.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
    <a class="btn btn-default" href="{% url 'event_attendance' pk=event.pk %}"><span class="glyphicon glyphicon-ok"></span></a>
    <h1>{{ event.title }}</h1>
    <p><u>Miesto</u>: {{ event.place }} </p>
    <p><u>Zaciatok</u>: {{ event.start }} </p>
    <p><u>Cena</u>: {{ event.price }} </p>
    <br>

    <p><u>Zucastneni:</u> {{attendance}}</p>
    <br>
    <p><u>Blizsie Informacie:</u></p>
    <p>{{ event.text|linebreaksbr }}</p>
  </div>
{% endblock %}

模板 - 我使用它的地方

{% load staticfiles %}

<html>
  <head>
      <title>Eventovy katalog</title>
      <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
      <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
       <link href="https://fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
       <link rel="stylesheet" href="{% static 'css/events.css' %}">
   </head>
     <body>
        <div class="page-header">
        {% if user.is_authenticated %}
          <a href="{% url 'event_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
        {% endif %}
        <h1><a href="/">Eventovy katalog</a></h1>
         </div>
        <div class="content container">
          <div class="row">
            <div class="col-md-8">
                {% block content %}
                {% endblock %}
            </div>
        </div>
    </div>
</body>

base.html文件

{% extends 'events/base.html' %}

{% block content %}
   <h1>Pridaj event</h1>
   <form method="POST" class="post-form">{% csrf_token %}
      {{ form.as_p }}
      <button type="submit" class="save btn btn-default">Save</button>
   </form>
{% endblock %}

post_edit.html

def event_attendance(request):
    event = get_object_or_404(Event)
    attendance = EventAttendance(person_key = request.user , 
    event_key = event.id, attended = True)
    attendance.save(force_insert=True)
    transaction.commit()
    return render(request, 'events/post_detail.html')

如果您需要任何其他详细信息,请告诉我并将其上传到某处。

感谢您的时间!

这是我的新功能,但我仍在努力将新行插入到我的数据库中。有什么建议吗?

com.databricks:spark-avro

1 个答案:

答案 0 :(得分:0)

解决方案在我的urls.py文件中:

以前的价值:

url(r'^post/(?P<pk>\d+)/$', views.event_attendance, name='event_attendance') 

新价值:

url(r'^post/(?P<pk>\d+)/attendance/$', views.event_attendance, name='event_attendance')

也许它将来会为某人提供服务!