Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Can you post your Event model? It's too hard to guess what you are trying to do without that. I have to assume a few things without it, so I'm sorry if I'm wrong.</p> <p>First off, I'm guessing that you should not be using an Event ModelForm for the EventSubscriptionForm. That doesn't really make sense. Hopefully, you created a through class for Event and User, so in your Event model, you have something like</p> <pre><code>subscriber_users = models.ManyToManyField(User, through="Subscription") </code></pre> <p>and</p> <pre><code>class Subscription(models.Model): user = models.ForeignKey(User, related_name="events",) event = models.ForeignKey(Event, related_name="subscribers") </code></pre> <p>Then you can use a Subscription ModelForm.</p> <p>Is there any reason you're using eventID instead of the django idiom, event_id? You should also import your Event and EventSubcribeForm classes with Pythonic casing. One <strong>very</strong> important thing is that you should be linking everything to User and not UserProfile.</p> <p>Technically, it makes more sense to set initial in the view rather than the form <strong>init</strong>, because you would have to pass request.user to <strong>init</strong> anyway.</p> <p>I think you should try this for your view...</p> <pre><code>@login_required def event_view(request, event_id=None): user = request.user.get_profile() event = Event.objects.get(id=event_id) initial = {'user': request.user} form = EventSubcriptionForm(request.POST or None, instance=event, initial=initial) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('event_list')) return render_to_response('event_view.html', { 'event': event, 'form': form }, context_instance = RequestContext(request)) </code></pre> <p>A few notes</p> <ul> <li>use request.user.get_profile() for the current user's profile object</li> <li>you can use <code>request.POST or None</code> to avoid the request.method cases</li> <li>always use named urls so you can reverse on names instead of hard-coding urls into views</li> <li>if you want <code>user</code> in your template context, just setup a context processor (see pinax for example on how to do this) instead of passing it in every single view. You can always use request.user also.</li> </ul> <p>Keep in mind that this code will only work if you have that through class setup like I said and you use a form like</p> <pre><code>class EventSubcriptionForm(forms.ModelForm): class Meta: model = Subscription exclude = ('event') </code></pre> <p><strong>EDIT</strong> Thanks a bunch for the ups. I'm not new to django, but somehow very new to SO.</p> <p>Okay, you should really read some of the PEPs about Python conventions <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow noreferrer">http://www.python.org/dev/peps/pep-0008/</a> or some SO posts about it <a href="https://stackoverflow.com/questions/159720/what-is-the-naming-convention-in-python-for-variable-and-function-names">What is the naming convention in Python for variable and function names?</a>.</p> <p>Here's what I recommend for your event app models.py:</p> <pre><code>class Event(models.Model): name = models.CharField(max_length=100) details = models.TextField() attendance_fee = models.FloatField(max_length=99) date = models.DateField() poster = models.ForeignKey(User, related_name='events_posted') deleted = models.BooleanField() attendee_users = models.ManyToManyField(User, through="Attendance") def __unicode__(self): return self.name class Attendance(models.Model): user = models.ForeignKey(User, related_name="events",) event = models.ForeignKey(Event, related_name="attendees") </code></pre> <p>Notes</p> <ul> <li>The name of a class is capitalized and singular. You are not describing events, you are the blueprint for an Event.</li> <li>you never need the name of the class in its attributes, i.e. event_name can just be name.</li> <li>all variables are lowercase_and_underscored</li> <li><strong>always</strong> link to User, not your profile model. A lot of django code expects this.</li> </ul> <p>So now you can access the users attending the event with event.attendees.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload