Django - how to follow some object (not user) [closed]

2024/11/15 0:30:12

Problem with follow button in django.

I have a web-site with database of some events. Registered users should be able to follow this events. I mean there should be a button "Follow" on an event page. Users who clicked it should see this event on their home page. But i just cant understand how it should work...

There is event model:

class Event(models.Model):name = models.CharField(max_length=26)second_name = models.CharField(max_length=60)description = models.CharField(max_length=200)type = models.CharField(max_length=3, default='cin')date = models.DateField()user = models.ForeignKey(User)image = models.ImageField(upload_to='images/',default='images/default.png')

And i have a view to output events on a page:

def home(request):all_events = Event.objects.all.order_by('date')return render(request, 'events/home.html', {'events': all_events})

in template -

{% block content %}<h1>{{ Event.name }}</h1><pre>{{ Event.date }}</pre><pre>{{ Event.description }}</pre>
{% endblock %}

I tried to use ManyToManyField like this but i dont understand exactly how it should work - what code should be in views.py, models.py and in template (button). Please help me

Answer

First step. I think, the relation should be ManyToMany . Event can be followed by multiple Users, and a User follow multiple Events.

model.py

class Event(models.Model):...users = models.ManyToManyField(User, blank=True)...

views.py user follow an Event

def follow(request,event_id):user = request.userevent = Event.objects.get(id=event_id)event.users.add(user)event.save()

urls.py

...
url(r'^event/(?P<event_id>\d+)/follow$', views.follow, name="event_follow"),
...

template.html

<a href="{% url 'event_follow' event.id %}">Follow</a>

or

<a href="/event/event_id/follow">Follow</a>
https://en.xdnf.cn/q/119697.html

Related Q&A

Python : wildcard query in Elasticsearch

I want to query a field using wildcard in Elasticsearch but the problem is that the search string is stored in a variable and not available statically. The intended query is : body={"query": …

Extracting text between two strings

I am looking to extract the text between two patterns in my text file here is my text:Q2 2016 Apple Inc Earnings Call - FinalONI SACCONAGHI, ANALYST, BERNSTEIN: I have one, and then a follow-up, as wel…

find find with file name as variable in glob

I want to delete specific files from a list of csv files. the file name is something like this: Storm.abc.ibtracs_all.v03r10.csv. where abc is different for each file.I have a list of the abc part (nam…

Remove Special Chars from a TSV file using Regex

I have a File called "X.tsv" i want to remove special characters (including double spaces) (excluding . Single spaces Tabs / -) using regex before i export them to sub files in python I want…

How to inherit a python base class?

dir/||___ __init__.py||___ Base_class.py||___ Subclass.py__init__.py is empty(as mentioned here)/* Base_class.pyclass Employee:numOfEmployees = 0 # Pure class member, no need to overrideraiseAmount = 1…

Validation for int(input()) python

def is_digit(x):if type(x) == int:return Trueelse:return Falsedef main():shape_opt = input(Enter input >> )while not is_digit(shape_opt):shape_opt = input(Enter input >> )else:print(it work…

How to get data out of a def function in python [duplicate]

This question already has answers here:How do I get ("return") a result (output) from a function? How can I use the result later?(4 answers)Closed 1 year ago.Trying to simplify lots of repe…

Calculating RSI in Python

I am trying to calculate RSI on a dataframedf = pd.DataFrame({"Close": [100,101,102,103,104,105,106,105,103,102,103,104,103,105,106,107,108,106,105,107,109]})df["Change"] = df["…

Pandas groupwise percentage

How can I calculate a group-wise percentage in pandas?similar to Pandas: .groupby().size() and percentages or Pandas Very Simple Percent of total size from Group by I want to calculate the percentage…

How to deal with large json files (flattening it to tsv) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…