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 }}
Related
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.
I have built a django website, which requires blog functionality. Rather than roll my own, I have decided to use django-zinnia, to provide blog functionality for my site.
I have managed to change the template to integrate more closely, with the pages on my existing site - however, there are still things I haven't managed yet - to do.
I have come come across zinnia template tags, but they are not giving me the full level of access that I need.
Specifically, I need to do the following:
Access to properties of post (i.e. "entries" in Zinnia lingo) and comments so that I can display the following items on my home page:
Show thumbnails of last N posts (or most popular posts for example)
Show last X comments
Programatically create new posts - and specify the state of the post, i.e. draft/published
Restrict viewing certain posts to authenticated users belonging to a specific django user group.
Am I able to get this tight integration between Zinnia and my django website - or am I better of writing my own blog app, with the requested features?
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
I've been searching stack overflow and google for a solution for over an hour now, and I can't seem to find something that, in my opinion, should be easy to obtain (as it's a common use case).
I've checked this thread, and a few others, but I haven't been able to find a real, easy solution:
Django modelform: is inline adding related model possible?
Anyway, say I have a model with three related entities, two foreign keys and a many-to-many related class. Now, I have a ModelForm which displays these in comboboxes and lists, but what I need is that "+" button next to these elements (as seen in the admin interface).
I want the plus to take me to a new form, for that particular entity, allow me to submit the new information, create the database entry, take me back to my original form and have the newly added entity selected in the combobox. I'm really hoping the django ModelForm Meta class has an attribute that I can't seem to find which enables exactly this.
This isn't really a django question.
This has to do with presentation of a particular widget in an html document, and that is governed by either the HTML markup, CSS, or javascript.
Django is a server side application and is primarily responsible for creating a valid http response and receiving a valid http request (of course, there is a lot that happens in the interim and that is why django is so big) but it's not a "one toolkit to kill them all" app.
I think you want to look at bootstrap: http://getbootstrap.com/
Jquery UI: http://jqueryui.com/
Or some combination of the two.
You can also just mark up the document yourself with a stock img or something.
However, if you want to do it exactly how the admin does it, just go into django.contrib.admin and examin the code to figure out how the django developers did it. I believe they are just using Jquery UI and some manual markup to accomplish that.
A designer recently handed me designs for a site I'm building for a client. They're great designs but I'm really scratching my head as to how I'm going to implement it on the site.
The content can very easily be broken down into blocks or chunks of data where I could allocate a textarea for text input, a couple of charfields for link-buttons, etc and sequentially render them out to the page.
The problem (eg why I'm not just pulling in Django-CMS et al) is the blocks are quite unique from each other. There are perhaps 20 different models that I would build for each block type. Rather than hack around a pre-fab CMS, I'd like to build a Page model and then just have an M2M that links to an ordered list of subclasses of my abstract Block class. I hope I'm not losing you.
I don't understand how I can do this. These questions spring to mind:
Is there a simple CMS that does all of this already? Am I wasting my time trying to figure out the physics?
My Blocks subclasses will technically be different type. Do I need generics for a M2M-through class to link to them? Is so, how do I do that?
How do I render x different forms in an inline admin context? (I'd like to have the page form with a list of the Blocks underneath)
How can the user specify the type of Block in the inline control?
Edit: Or, alternatively, what about a templatetag-based CMS?
I'm thinking of something like plonking this in my template:
{% editable 'unique_id' 'content-type' %}
A further example:
{% editable 'home-intro' 'text' %}
Then I could just stick these around the templates I want to be editable, in the way I want them to be editable and when logged in the client would see "Edit text", "Edit link", "Edit image" links which simply popped up the right field.
This would make things more locked down but the layout needs to remain solid (and the client knows nothing about HTML/CSS) so it's one or other of these methods IMO.
Couldn't you implement your 'Blocks' as Django CMS Plugins? Then each page is just constructed from a number of plugins.
Each plugin has an admin form which gets the specifics for itself, and then the page template renders the plugin as you want it.
If you look at the first page of the django-cms demo:
https://www.django-cms.org/en/tour-demo/
you'll see in (1) a highlighted plugin block - in this case a formatted text block that is edited with TinyMCE or similar. You can define your own plugins and add them to pages.
last month I published an article (for review) on how tho build a basic CMS for Jinja. This's templating language does not dffer very much from Django, which I have been using before.
You can find it here. It uses template inheritance to fill the content blocks.
https://codereview.stackexchange.com/questions/5965/review-request-jinja-cms-for-energiekantoor-nl-on-google-app-engine
Or type in Google : Jinja CMS