Browse Source

adding new tags from student_page to backend

newsletter
Esther Kleinhenz 6 years ago
parent
commit
5a30a0adea
4 changed files with 37 additions and 12 deletions
  1. 9
    2
      application/forms.py
  2. 2
    2
      application/models.py
  3. 6
    0
      application/templates/student_page.html
  4. 20
    8
      application/views.py

+ 9
- 2
application/forms.py View File

@@ -1,8 +1,15 @@
from django import forms
from .models import Post, CustomUser
from taggit.forms import *
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

class PostForm(forms.ModelForm):
published_date = forms.DateField(widget=forms.widgets.DateInput(format="%d. %h %Y, %M:%S"))
class Meta:
model = Post
fields = ('title', 'text', 'published_date', 'tags')
fields = ('title', 'text', 'published_date', 'tags')

class NewTagForm(forms.ModelForm):
m_tags = TagField()
class Meta:
model = CustomUser
fields = ['m_tags']

+ 2
- 2
application/models.py View File

@@ -4,8 +4,8 @@ from django.utils import timezone
from taggit.managers import TaggableManager

class CustomUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
mytags = TaggableManager()
user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
tags = TaggableManager()


class Post(models.Model):

+ 6
- 0
application/templates/student_page.html View File

@@ -10,4 +10,10 @@
<table class="table table-striped table-bordered table-hover">
</table>
</div>

<form class="post-form" method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="save btn btn-outline-dark">Save</button>
</form>
{% endblock %}

+ 20
- 8
application/views.py View File

@@ -1,7 +1,7 @@
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Post
from .forms import PostForm
from .models import Post, CustomUser
from .forms import PostForm, NewTagForm
from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
@@ -111,12 +111,24 @@ def post_remove(request, pk):

@login_required
def student_page(request):
q = request.GET.get('q')
query = Q()
for word in q or []:
query = query | Q(tags__name__icontains=word)
result = Post.objects.filter(query)
return render(request, 'blog_search_list_view.html', {'result':result})
user_instance = CustomUser.objects.get(user=request.user)
if request.method == "POST":
form = NewTagForm(request.POST, instance=user_instance)
if form.is_valid():
obj = form.save(commit=False)
obj.user = request.user
obj.save()
m_tags = form.cleaned_data['m_tags']
obj.tags.add(*m_tags)
form.save_m2m()
return redirect('/student/')

else:
form = NewTagForm()
return render(request, 'student_page.html', {'form':form})



class TagSearch(TagCanvasListView):

Loading…
Cancel
Save