Reputation: 1527
I have a Django app that has a list of companies, and when the user clicks on a company they are presented with all of the details of that company. Now I'm trying to add a tag feature so any use can add tags to the company which they'll also be able to search on. I've created the form and the template stuff, but now I'm stuck now on how to wire it into the View, specifically updating the Company model with the new tag(s).
Forms.py
class TagForm(forms.Form):
tag = forms.CharField(label='Tag', max_length=100)
class Meta:
model = Company
field = 'tag'
Template
<div class="add-tag-form">
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default btn-sm">Add</button>
</form>
</div>
View
class CompanyDetails(generic.DetailView):
model = Company
template_name = 'company_details.html'
# This was my attempt to work it into the View...
def post(self, request, *args, **kwargs):
form = TagForm(request.POST)
if form.is_valid():
tag = form.save(commit=False)
tag.company = Company.objects.get(pk=pk)
tag.save()
def get_context_data(self, **kwargs):
pk = self.kwargs.get('pk')
context = super(CompanyDetails, self).get_context_data(**kwargs)
context['articles'] = Articles.objects.filter(company_id=pk).order_by('-date')
context['transcripts'] = Transcripts.objects.filter(company_id=pk).order_by('-date')
context['form'] = TagForm()
# Yahoo Finance API data
stock_symbol = self.object.stock_symbol
data = Share(stock_symbol)
stock_open = data.get_open()
year_range = data.get_year_range()
fifty_day_moving_average = data.get_50day_moving_avg()
market_cap = data.get_market_cap()
yahoo_finance = dict()
yahoo_finance['stock_open'] = stock_open
yahoo_finance['year_range'] = year_range
yahoo_finance['fifty_day_moving_average'] = fifty_day_moving_average
yahoo_finance['market_cap'] = market_cap
context['yahoo_finance'] = yahoo_finance
# Yahoo News RSS data
yahoo_news = feedparser.parse("http://finance.yahoo.com/rss/headline?s=" + stock_symbol)
yahoo_news = yahoo_news['entries']
for item in yahoo_news:
item['published'] = datetime.strptime(item['published'], '%a, %d %b %Y %H:%M:%S %z')
context['yahoo_news'] = yahoo_news
return context
UPDATE
The error I get "'TagForm' object has no attribute 'save'". When I change the form from forms.Form to forms.FormModel I get the following error "Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form TagForm needs updating.". I'm not trying to create a model, rather update it through the form.
Taggit is used in the Model
class Company(models.Model):
company_name = models.CharField(max_length=200)
tags = TaggableManager()
Upvotes: 0
Views: 663
Reputation: 1521
One thing that you can do is to make a simple form for the tag, just like what you did but remove the Meta information and update the post method to just add the tag in the company. Something like below:
# forms.py
class TagForm(forms.Form):
tag = forms.CharField(label='Tag', max_length=100)
# views.py
class CompanyDetails(generic.DetailView):
...
# This was my attempt to work it into the View...
def post(self, request, *args, **kwargs):
company = self.get_object()
form = TagForm(request.POST)
if form.is_valid():
company.tags.add(form.cleaned_data['tag'])
return self.render_to_response(self.get_context_data())
Upvotes: 1