models py TAG_FIELD director Режиссер genre Жанр casting ролях word сл

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//models.py
TAG_FIELD = (
('director', 'Режиссер'),
('genre', 'Жанр'),
('casting', 'В ролях'),
('word', 'слово'),
)
class Tag(models.Model):
field_name = models.CharField(max_length=20, choices=TAG_FIELD)
tag_name = models.CharField(max_length=40)
def __unicode__(self):
return "%s - %s" % (field_name, tag_name)
class Topic(models.Model):
title = models.CharField(max_length=20)
body = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)
tags = models.ManyToManyField(Tag)
def __unicode__(self):
return self.title
//forms.py
class TagForm(forms.ModelForm):
class Meta:
model = Tag
def save(self):
field_name = self.cleaned_data.get("field_name")
tag_name = self.cleaned_data.get("tag_name")
try:
obj = Tag.objects.get(field_name=field_name, tag_name=tag_name)
except DoesNotExist:
obj = Tag.objects.create(field_name=field_name, tag_name=tag_name)
FIELDS = (
('director', 'Режиссер'),
('genre', 'Жанр'),
('casting', 'В ролях'),
('word', 'слово'),
('group', 'группа'),
('year', 'Год'),
)
class SearchByTagForm(forms.ModelForm):
field_name = forms.ChoiceField(max_length=20, choices=FIELDS)
tag_name = forms.CharField(max_length=30)
//views.py
def search_by_tag(request):
if request.method == 'POST':
form = SearchByTagForm(request.POST)
if form.is_valid():
field_name = form.cleaned_data.get("field_name")
tag_name = form.cleaned_data.get("tag_name")
objects = Topic.objects.filter(field_name=field_name, tag_name=tag_name)
else:
form = SearchByTagForm()
return render_to_response("search.html", {'objects': objects})
def get_by_tag(request):
objects = Topic.objects.filter(
return render_to_response("fuck.html"), {})