share button for own site Django - python

I want to implement a ''share on my profile'' button on my page but I have a hard time how to do so. When I try to find something like that (google/here) the results are "how to share something from your site on twitter/facebook". If there is a similar question please share the link I was not able to find something.
So i have a site where users have a profile where the liked content is displayed and I would like to give the user the option to share a Post on his own profile with a personal comment(like FB/twitter does).
My problem is that I don't know how to implement this into my models. If I want to save the "shared" post in a QuerySet in the UserProfile model I don't know where to save the comment for each shared post. If I make an extra table for all the shared posts with a extra form combined its a total mess since each post is saved individually and I don't know how to combine the existing image in to the form where the user writes his comment .
Can anybody tell me in which direction I have to walk? Feeling a bit lost on this one.

You don't need to make an extra table, django will do it for you, if you'll use ManyToManyField in your Post model.
See this docs https://docs.djangoproject.com/en/1.10/ref/models/fields/#manytomanyfield

Related

mentioning or tagging users or any models in django

Let's say I have a django project of a social network and this site has many users. All the users come from django.contrib.auth.models.User class. Now one user let's say writes a blog and in it, mentions another user with '#' similar to of twitter. So exactly how to do this? What kind of approach should be taken?
And coming away from just user model, rather how to do this with any custom model also? Like if I have a Blog model and each blog has a title, how to mention with that title? And most importantly this mentioning should be an automatic thing. Like If one user's name is "PhantomWarrior", then if one writes "#Phant" and he is still writing, it should automatically predict the username "PhantomWarrior" and give the user option to select that for mentioning.
Similarly how to do this with the title of a blog?
I found this post: how to mention/tag users with '#' on a django developed project in stackoverflow talking about this thing but the answer kind of did not satisfy what I am wanting.
So how to do this? Any help will be much appreciated.
You can do it in two ways.
In any cases you need add m2m field from blog post to user model.
Simplest solution. After any creation or changes in model you need to check content and parse text for all #NAME patterns, for example using regexp. After this you need search all founded patterns in user model and add it to m2m field in blog post.
This solution used in most cases like you wrote. You need add frontend component, that open autocomplete every time, when you write # in text boxes. This autocomplete component should be connected to api method, that returns list of users names and IDs, by search pattern. When you select user in autocomplete you need store his ID in some storage on frontend side. When form will be send, you send user IDs with all other form data, and save it in m2m field in blog post.

Django Block URL by Username

Excuse me if I'm not very direct about what I'm talking about or need, I'm fairly new to django/python and am still in the process of understanding things.
I am creating a webapp with Django where a user will sign up, log in, create a new entry, save it, and view/edit it. Think of it as a personal diary that should be only accessible to the user who wrote it.
I've gotten pretty far in this and am using filtering in the ListView, but someone could easily use url manipulation to find other users entries.
Such that User A posts entry 4 at site.com/entry/4 and then user B is able to type that in and see the entry.
How can I go about restricting the url to only the user who posted it?
Thanks for any help, and again sorry if I'm not giving enough information or I'm not clear on what I am talking about!

Make user-submitted links live (i.e. clickable) in a django app

This question is for django ninjas: I have an app (djagno 1.4) that allows users to create posts. These posts have a number of fields, including several CharFields.
My problem is this: when a user creates a new post and pastes a live link into one of these post CharFields, I want that link to be live and clickable as soon as its up. On doing some research I saw here how to deal with the same problem in php.
Can someone help me out, or point me in the right direction to some documentation? I can't find any on this topic.
What you want is the urlize filter, so, let's say your Post model has a field called text, then in your template you'd do this:
{{ post_obj.text|urlize }}

Thumbnails of ImageField of Many-to-Many relationship list in the admin interface

I have two models, Post and Photo. Photo contains information about photos, the date the original photo was uploaded (or alternatively - shot), a description, a title and more. The post contains post text and a ManyToManyField which links back to photos to see which are related to the post.
I however have a problem that, seeing all the photos' titles in list view makes very little sense, and so I must look into a way to show previews.
I have sorl-thumbnails working, but I'm not sure where to start in order to get them into the admin interface.
Help will be very much appreciated!

Processing forms that generate many rows in DB

I'm wondering what the best approach to take here is. I've got a form that people use to register for a class and a lot of times the manager of a company will register multiple people for the class at the same time. Presently, they'd have to go through the registration process multiple times and resubmit the form once for every person they want to register.
What I want to do is give the user a form that has a single <input/> for one person to register with, along with all the other fields they'll need to fill out (Email, phone number, etc); if they want to add more people, they'll be able to press a button and a new <input/> will be generated. This part I know how to do, but I'm including it to best describe what I'm aiming to do.
The part I don't know how to approach is processing that data the form submits, I need some way of making a new row in the Registrant table for every <input/> that's added and include the same contact information (phone, email, etc) as the first row with that row. For the record, I'm using the Django framework for my back-end code.
What's the best approach here? Should it just POST the form x times for x people, or is there a less "brute force" way of handling this?
Django includes FormSet for dealing with exactly these challenges. Using a FormSet you can create multiple forms for creating or updating information. There's even possible to generate the FormSets from a Model. http://docs.djangoproject.com/en/dev/topics/forms/formsets/ and http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#id1 are great resources.
Now, for creating more forms on the fly, you need some javascript magic. I've done this on work projects using jQuery which made it a lot simpler. The basic idea is create a new form with the correct inputs and change the hidden metadata in the formset form so it will now how many forms to process. The admin implements this when using multiple inline forms so I suggest looking there for code as it is a bit tricky to get right.

Categories

Resources