How to bookmark a page in Django?Django, save ModelFormDjango Save method needs to update model instance twiceTrying to get Django and jQuery working together to add/remove bookmarks via an “Add/Remove Bookmark” linkDjango set bookmark for url querystringHow can I represent an 'Enum' in Python?What is a “slug” in Django?How do I do a not equal in Django queryset filtering?Does Django scale?differentiate null=True, blank=True in djangodjango database error when linking one to one to the user modelDjango 1.9 send value to view from formDjango : HTML form action directing to view (or url?) with 2 argumentsDjango 404 Page not loadingget data based on primary key and display in html
Why doesn't Starship have four landing legs?
Are there any to-scale diagrams of the TRAPPIST-1 system?
Debian editor with syntax highlight for DOT language
Heat output from a 200W electric radiator?
Another "Ask One Question" Question
Why is 3/4 a simple meter while 6/8 is a compound meter?
Why can't you say don't instead of won't?
web scraping images
Is the internet in Madagascar faster than in UK?
In Endgame, wouldn't Stark have remembered Hulk busting out of the stairwell?
Shall I fix cracks on bathtub and how to fix them?
Is this password scheme legit?
Is there a word or phrase that means "use other people's wifi or Internet service without consent"?
Why does a sticker slowly peel off, but if it is pulled quickly it tears?
Why is there not a willingness from the world to step in between Pakistan and India?
Why can't I identify major and minor chords?
Why does glibc's strlen need to be so complicated to run quickly?
Did ancient peoples ever hide their treasure behind puzzles?
Spicing up a moment of peace
Why is there no Disney logo in MCU movies?
How to prevent a hosting company from accessing a VM's encryption keys?
Is there a better way to use C# dictionaries than TryGetValue?
Can I get a PhD for developing educational software?
Are spot colors limited and why CMYK mix is not treated same as spot color mix?
How to bookmark a page in Django?
Django, save ModelFormDjango Save method needs to update model instance twiceTrying to get Django and jQuery working together to add/remove bookmarks via an “Add/Remove Bookmark” linkDjango set bookmark for url querystringHow can I represent an 'Enum' in Python?What is a “slug” in Django?How do I do a not equal in Django queryset filtering?Does Django scale?differentiate null=True, blank=True in djangodjango database error when linking one to one to the user modelDjango 1.9 send value to view from formDjango : HTML form action directing to view (or url?) with 2 argumentsDjango 404 Page not loadingget data based on primary key and display in html
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to edit an html page so a logged in user can favorite / bookmark a video.id
Here is the .html file
<td>
<form method='POST' action="% url 'researcher_view_app:favourite_post' video.id %">
% csrf_token %
<input type='hidden' name='video' value= video.id >
<button type='submit'>Bookmark</button>
</form>
</td>
Here is the urls.py file
path('<int:fav_id>/favourite_post', views.favourite_post, name='favourite_post'),
Here is the view.py file
def favourite_post(request, fav_id):
video = get_object_or_404(Video, id=fav_id)
if request.method == 'POST':
video.
return render(request, 'researcher_view_app/%s' % fav_id)
django python-3.x django-models django-forms django-views
add a comment |
I am trying to edit an html page so a logged in user can favorite / bookmark a video.id
Here is the .html file
<td>
<form method='POST' action="% url 'researcher_view_app:favourite_post' video.id %">
% csrf_token %
<input type='hidden' name='video' value= video.id >
<button type='submit'>Bookmark</button>
</form>
</td>
Here is the urls.py file
path('<int:fav_id>/favourite_post', views.favourite_post, name='favourite_post'),
Here is the view.py file
def favourite_post(request, fav_id):
video = get_object_or_404(Video, id=fav_id)
if request.method == 'POST':
video.
return render(request, 'researcher_view_app/%s' % fav_id)
django python-3.x django-models django-forms django-views
thank you downvoter with no explanation
– Matthew
Mar 27 at 20:36
What's your mechanism to store a "favorite" here, another model class? There's not enough information in this question to provide an answer to a specific question. Please add enough context so that someone can point you toward a solution.
– jonafato
Mar 31 at 5:05
add a comment |
I am trying to edit an html page so a logged in user can favorite / bookmark a video.id
Here is the .html file
<td>
<form method='POST' action="% url 'researcher_view_app:favourite_post' video.id %">
% csrf_token %
<input type='hidden' name='video' value= video.id >
<button type='submit'>Bookmark</button>
</form>
</td>
Here is the urls.py file
path('<int:fav_id>/favourite_post', views.favourite_post, name='favourite_post'),
Here is the view.py file
def favourite_post(request, fav_id):
video = get_object_or_404(Video, id=fav_id)
if request.method == 'POST':
video.
return render(request, 'researcher_view_app/%s' % fav_id)
django python-3.x django-models django-forms django-views
I am trying to edit an html page so a logged in user can favorite / bookmark a video.id
Here is the .html file
<td>
<form method='POST' action="% url 'researcher_view_app:favourite_post' video.id %">
% csrf_token %
<input type='hidden' name='video' value= video.id >
<button type='submit'>Bookmark</button>
</form>
</td>
Here is the urls.py file
path('<int:fav_id>/favourite_post', views.favourite_post, name='favourite_post'),
Here is the view.py file
def favourite_post(request, fav_id):
video = get_object_or_404(Video, id=fav_id)
if request.method == 'POST':
video.
return render(request, 'researcher_view_app/%s' % fav_id)
django python-3.x django-models django-forms django-views
django python-3.x django-models django-forms django-views
edited Mar 28 at 12:39
Matthew
asked Mar 27 at 18:28
MatthewMatthew
3454 silver badges20 bronze badges
3454 silver badges20 bronze badges
thank you downvoter with no explanation
– Matthew
Mar 27 at 20:36
What's your mechanism to store a "favorite" here, another model class? There's not enough information in this question to provide an answer to a specific question. Please add enough context so that someone can point you toward a solution.
– jonafato
Mar 31 at 5:05
add a comment |
thank you downvoter with no explanation
– Matthew
Mar 27 at 20:36
What's your mechanism to store a "favorite" here, another model class? There's not enough information in this question to provide an answer to a specific question. Please add enough context so that someone can point you toward a solution.
– jonafato
Mar 31 at 5:05
thank you downvoter with no explanation
– Matthew
Mar 27 at 20:36
thank you downvoter with no explanation
– Matthew
Mar 27 at 20:36
What's your mechanism to store a "favorite" here, another model class? There's not enough information in this question to provide an answer to a specific question. Please add enough context so that someone can point you toward a solution.
– jonafato
Mar 31 at 5:05
What's your mechanism to store a "favorite" here, another model class? There's not enough information in this question to provide an answer to a specific question. Please add enough context so that someone can point you toward a solution.
– jonafato
Mar 31 at 5:05
add a comment |
1 Answer
1
active
oldest
votes
- First you modify the models.py that has the user models
class ProjectUser(AbstractUser):
images = models.ManyToManyField(Images)
def __str__(self):
return self.email
- In the .html file add the following:
% for image in available_images %
/* show image */
<form method='post' action="% url 'user-image-add' %">
% csrf_token %
<input type='hidden' name='image' value=image.id>
<button type='submit'>bookmark</button>
</form>
% endfor %
- In your views.py add the following method
def user_image_add(request):
user_image_form = ImageForm(request.POST or None)
if request.method == 'POST' and user_image_form.is_valid():
request.user.images.add(user_image_form.cleaned_data['image'])
return JsonResponse(status=200)
raise Http404()
- Create a forms.py file in your add and add the following:
class ImageForm(forms.Form):
image = forms.ModelChoiceField(queryset=Images.objects.all())
To show those bookmarked images you can just iterate over request.user.images
(it gives you a QS of Images) similar to code above.
- In the urls.py add the following:
path('user-image-add/', views.user_image_add, 'user-image-add')
- In models.py add a method in User model for getting bool if video is bookmarked
def is_bookmarked(self, video_id):
return self.bookmarked_videos.filter(id=video_id).exists()
simirlarly is_bookmarked
can be added to Video model accepting user_id and checking video.projectuser_set
.
And add the following to your .html file where users bookmarked a video
`% if video.is_bookmarked %`
- Delete the
UserProfile
as you do not need it. Just make sure to have needed instance in context of view.
i updated answer to bookmark images. Considering you have a model for Video somewhere in your code, I think you need to make m2m to it and just haveVideo.image = FileField
to show to user.
– Kyryl Havrylenko
Mar 27 at 18:56
i find func based views way simpler to deal with, if you want cbv you can decide what class to inherit yourself and redo it, it's only a couple of lines.
– Kyryl Havrylenko
Mar 27 at 20:18
edited my answer to answer your updated question on how to check if a video is bookmarked
– Kyryl Havrylenko
Mar 27 at 20:22
updated it. Next you should put the code in your project and fix small things that are not visible on the scale of SO question yourself.
– Kyryl Havrylenko
Mar 28 at 9:23
image.id is in form body, it's available in request.POST if you want to get raw data. Form parses it, validates and saves to 'cleaned_data' onceis_valid
is called
– Kyryl Havrylenko
Mar 28 at 9:29
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384218%2fhow-to-bookmark-a-page-in-django%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
- First you modify the models.py that has the user models
class ProjectUser(AbstractUser):
images = models.ManyToManyField(Images)
def __str__(self):
return self.email
- In the .html file add the following:
% for image in available_images %
/* show image */
<form method='post' action="% url 'user-image-add' %">
% csrf_token %
<input type='hidden' name='image' value=image.id>
<button type='submit'>bookmark</button>
</form>
% endfor %
- In your views.py add the following method
def user_image_add(request):
user_image_form = ImageForm(request.POST or None)
if request.method == 'POST' and user_image_form.is_valid():
request.user.images.add(user_image_form.cleaned_data['image'])
return JsonResponse(status=200)
raise Http404()
- Create a forms.py file in your add and add the following:
class ImageForm(forms.Form):
image = forms.ModelChoiceField(queryset=Images.objects.all())
To show those bookmarked images you can just iterate over request.user.images
(it gives you a QS of Images) similar to code above.
- In the urls.py add the following:
path('user-image-add/', views.user_image_add, 'user-image-add')
- In models.py add a method in User model for getting bool if video is bookmarked
def is_bookmarked(self, video_id):
return self.bookmarked_videos.filter(id=video_id).exists()
simirlarly is_bookmarked
can be added to Video model accepting user_id and checking video.projectuser_set
.
And add the following to your .html file where users bookmarked a video
`% if video.is_bookmarked %`
- Delete the
UserProfile
as you do not need it. Just make sure to have needed instance in context of view.
i updated answer to bookmark images. Considering you have a model for Video somewhere in your code, I think you need to make m2m to it and just haveVideo.image = FileField
to show to user.
– Kyryl Havrylenko
Mar 27 at 18:56
i find func based views way simpler to deal with, if you want cbv you can decide what class to inherit yourself and redo it, it's only a couple of lines.
– Kyryl Havrylenko
Mar 27 at 20:18
edited my answer to answer your updated question on how to check if a video is bookmarked
– Kyryl Havrylenko
Mar 27 at 20:22
updated it. Next you should put the code in your project and fix small things that are not visible on the scale of SO question yourself.
– Kyryl Havrylenko
Mar 28 at 9:23
image.id is in form body, it's available in request.POST if you want to get raw data. Form parses it, validates and saves to 'cleaned_data' onceis_valid
is called
– Kyryl Havrylenko
Mar 28 at 9:29
|
show 3 more comments
- First you modify the models.py that has the user models
class ProjectUser(AbstractUser):
images = models.ManyToManyField(Images)
def __str__(self):
return self.email
- In the .html file add the following:
% for image in available_images %
/* show image */
<form method='post' action="% url 'user-image-add' %">
% csrf_token %
<input type='hidden' name='image' value=image.id>
<button type='submit'>bookmark</button>
</form>
% endfor %
- In your views.py add the following method
def user_image_add(request):
user_image_form = ImageForm(request.POST or None)
if request.method == 'POST' and user_image_form.is_valid():
request.user.images.add(user_image_form.cleaned_data['image'])
return JsonResponse(status=200)
raise Http404()
- Create a forms.py file in your add and add the following:
class ImageForm(forms.Form):
image = forms.ModelChoiceField(queryset=Images.objects.all())
To show those bookmarked images you can just iterate over request.user.images
(it gives you a QS of Images) similar to code above.
- In the urls.py add the following:
path('user-image-add/', views.user_image_add, 'user-image-add')
- In models.py add a method in User model for getting bool if video is bookmarked
def is_bookmarked(self, video_id):
return self.bookmarked_videos.filter(id=video_id).exists()
simirlarly is_bookmarked
can be added to Video model accepting user_id and checking video.projectuser_set
.
And add the following to your .html file where users bookmarked a video
`% if video.is_bookmarked %`
- Delete the
UserProfile
as you do not need it. Just make sure to have needed instance in context of view.
i updated answer to bookmark images. Considering you have a model for Video somewhere in your code, I think you need to make m2m to it and just haveVideo.image = FileField
to show to user.
– Kyryl Havrylenko
Mar 27 at 18:56
i find func based views way simpler to deal with, if you want cbv you can decide what class to inherit yourself and redo it, it's only a couple of lines.
– Kyryl Havrylenko
Mar 27 at 20:18
edited my answer to answer your updated question on how to check if a video is bookmarked
– Kyryl Havrylenko
Mar 27 at 20:22
updated it. Next you should put the code in your project and fix small things that are not visible on the scale of SO question yourself.
– Kyryl Havrylenko
Mar 28 at 9:23
image.id is in form body, it's available in request.POST if you want to get raw data. Form parses it, validates and saves to 'cleaned_data' onceis_valid
is called
– Kyryl Havrylenko
Mar 28 at 9:29
|
show 3 more comments
- First you modify the models.py that has the user models
class ProjectUser(AbstractUser):
images = models.ManyToManyField(Images)
def __str__(self):
return self.email
- In the .html file add the following:
% for image in available_images %
/* show image */
<form method='post' action="% url 'user-image-add' %">
% csrf_token %
<input type='hidden' name='image' value=image.id>
<button type='submit'>bookmark</button>
</form>
% endfor %
- In your views.py add the following method
def user_image_add(request):
user_image_form = ImageForm(request.POST or None)
if request.method == 'POST' and user_image_form.is_valid():
request.user.images.add(user_image_form.cleaned_data['image'])
return JsonResponse(status=200)
raise Http404()
- Create a forms.py file in your add and add the following:
class ImageForm(forms.Form):
image = forms.ModelChoiceField(queryset=Images.objects.all())
To show those bookmarked images you can just iterate over request.user.images
(it gives you a QS of Images) similar to code above.
- In the urls.py add the following:
path('user-image-add/', views.user_image_add, 'user-image-add')
- In models.py add a method in User model for getting bool if video is bookmarked
def is_bookmarked(self, video_id):
return self.bookmarked_videos.filter(id=video_id).exists()
simirlarly is_bookmarked
can be added to Video model accepting user_id and checking video.projectuser_set
.
And add the following to your .html file where users bookmarked a video
`% if video.is_bookmarked %`
- Delete the
UserProfile
as you do not need it. Just make sure to have needed instance in context of view.
- First you modify the models.py that has the user models
class ProjectUser(AbstractUser):
images = models.ManyToManyField(Images)
def __str__(self):
return self.email
- In the .html file add the following:
% for image in available_images %
/* show image */
<form method='post' action="% url 'user-image-add' %">
% csrf_token %
<input type='hidden' name='image' value=image.id>
<button type='submit'>bookmark</button>
</form>
% endfor %
- In your views.py add the following method
def user_image_add(request):
user_image_form = ImageForm(request.POST or None)
if request.method == 'POST' and user_image_form.is_valid():
request.user.images.add(user_image_form.cleaned_data['image'])
return JsonResponse(status=200)
raise Http404()
- Create a forms.py file in your add and add the following:
class ImageForm(forms.Form):
image = forms.ModelChoiceField(queryset=Images.objects.all())
To show those bookmarked images you can just iterate over request.user.images
(it gives you a QS of Images) similar to code above.
- In the urls.py add the following:
path('user-image-add/', views.user_image_add, 'user-image-add')
- In models.py add a method in User model for getting bool if video is bookmarked
def is_bookmarked(self, video_id):
return self.bookmarked_videos.filter(id=video_id).exists()
simirlarly is_bookmarked
can be added to Video model accepting user_id and checking video.projectuser_set
.
And add the following to your .html file where users bookmarked a video
`% if video.is_bookmarked %`
- Delete the
UserProfile
as you do not need it. Just make sure to have needed instance in context of view.
edited Mar 28 at 9:22
answered Mar 27 at 18:44
Kyryl HavrylenkoKyryl Havrylenko
4642 silver badges9 bronze badges
4642 silver badges9 bronze badges
i updated answer to bookmark images. Considering you have a model for Video somewhere in your code, I think you need to make m2m to it and just haveVideo.image = FileField
to show to user.
– Kyryl Havrylenko
Mar 27 at 18:56
i find func based views way simpler to deal with, if you want cbv you can decide what class to inherit yourself and redo it, it's only a couple of lines.
– Kyryl Havrylenko
Mar 27 at 20:18
edited my answer to answer your updated question on how to check if a video is bookmarked
– Kyryl Havrylenko
Mar 27 at 20:22
updated it. Next you should put the code in your project and fix small things that are not visible on the scale of SO question yourself.
– Kyryl Havrylenko
Mar 28 at 9:23
image.id is in form body, it's available in request.POST if you want to get raw data. Form parses it, validates and saves to 'cleaned_data' onceis_valid
is called
– Kyryl Havrylenko
Mar 28 at 9:29
|
show 3 more comments
i updated answer to bookmark images. Considering you have a model for Video somewhere in your code, I think you need to make m2m to it and just haveVideo.image = FileField
to show to user.
– Kyryl Havrylenko
Mar 27 at 18:56
i find func based views way simpler to deal with, if you want cbv you can decide what class to inherit yourself and redo it, it's only a couple of lines.
– Kyryl Havrylenko
Mar 27 at 20:18
edited my answer to answer your updated question on how to check if a video is bookmarked
– Kyryl Havrylenko
Mar 27 at 20:22
updated it. Next you should put the code in your project and fix small things that are not visible on the scale of SO question yourself.
– Kyryl Havrylenko
Mar 28 at 9:23
image.id is in form body, it's available in request.POST if you want to get raw data. Form parses it, validates and saves to 'cleaned_data' onceis_valid
is called
– Kyryl Havrylenko
Mar 28 at 9:29
i updated answer to bookmark images. Considering you have a model for Video somewhere in your code, I think you need to make m2m to it and just have
Video.image = FileField
to show to user.– Kyryl Havrylenko
Mar 27 at 18:56
i updated answer to bookmark images. Considering you have a model for Video somewhere in your code, I think you need to make m2m to it and just have
Video.image = FileField
to show to user.– Kyryl Havrylenko
Mar 27 at 18:56
i find func based views way simpler to deal with, if you want cbv you can decide what class to inherit yourself and redo it, it's only a couple of lines.
– Kyryl Havrylenko
Mar 27 at 20:18
i find func based views way simpler to deal with, if you want cbv you can decide what class to inherit yourself and redo it, it's only a couple of lines.
– Kyryl Havrylenko
Mar 27 at 20:18
edited my answer to answer your updated question on how to check if a video is bookmarked
– Kyryl Havrylenko
Mar 27 at 20:22
edited my answer to answer your updated question on how to check if a video is bookmarked
– Kyryl Havrylenko
Mar 27 at 20:22
updated it. Next you should put the code in your project and fix small things that are not visible on the scale of SO question yourself.
– Kyryl Havrylenko
Mar 28 at 9:23
updated it. Next you should put the code in your project and fix small things that are not visible on the scale of SO question yourself.
– Kyryl Havrylenko
Mar 28 at 9:23
image.id is in form body, it's available in request.POST if you want to get raw data. Form parses it, validates and saves to 'cleaned_data' once
is_valid
is called– Kyryl Havrylenko
Mar 28 at 9:29
image.id is in form body, it's available in request.POST if you want to get raw data. Form parses it, validates and saves to 'cleaned_data' once
is_valid
is called– Kyryl Havrylenko
Mar 28 at 9:29
|
show 3 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384218%2fhow-to-bookmark-a-page-in-django%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
thank you downvoter with no explanation
– Matthew
Mar 27 at 20:36
What's your mechanism to store a "favorite" here, another model class? There's not enough information in this question to provide an answer to a specific question. Please add enough context so that someone can point you toward a solution.
– jonafato
Mar 31 at 5:05