Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I was having a lot of trouble getting Django running on windows until I found this guide which sped things up. The official guide just wasn't saying how to do this installation on windows very well.

I am a product of Rails and have been running through a few tutorials here. I am new to this so I'll be back with a few questions later on once I've finished exploring a little more. Impressed with some of the things it does and unimpressed with the difficulty involved in doing them. Perhaps I don't understand the purpose in Django's complexity yet but maybe some alias' are in order, those are just my first impressions.

Nolgthorn fucked around with this message at 20:11 on Mar 6, 2008

Adbot
ADBOT LOVES YOU

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Is there any reason to put your template files outside of the project directory?

Specs seem to encourage this, but they also mention that each app of your project is checked for a templates folder just in case.

I am for some reason having trouble using generic views with app/templates folders. My project seems to be looking at app/templates/app/template.html, where the second /app shouldn't be there.

OP, the admin interface is cool and all but it reminds me of scaffolding in Rails. It's nothing nearly as fascinating as Django's generic views, the automatically generated authentication and permissions system or Django's development web server never needing to be restarted.

It's awesome :v:

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Yes, I have. I was curious as to when you would want to use the app/templates folder instead of using the one referenced in settings... Also I was just bringing it up for discussion how generic views don't seem to support the app/templates folders, only the settings referenced ones.

The other really cool feature about Django, does it actually go through to figure out exactly what data will be needed then gets it all at once? I don't have to explicitly tell the application ahead of time what all will be needed?

I just want to check with you guys to see if that's true, it seems too good.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Bonus posted:

So if you have
code:
my_results = YourModel.objects.all() #not evaluated, no DB access
my_results = my_results.filter(something='blah') #not evaluated, no DB access
my_results = my_results.filter(something_else='foo') #not evaluated, no DB access
result = my_results[0] #evaluated, DB is hit
all_results = list(my_results) #evaluated, DB is hit

In your example would Django not look ahead and see that you wanted 'all_results', therefore only have to hit the database once to get it and return the value of 'result' from that?

What if you have
code:
my_results = YourModel.objects.all() #not evaluated, no DB access
my_results = my_results.filter(something='blah') #not evaluated, no DB access
my_results = my_results.filter(something_else='foo') #not evaluated, no DB access

all_results = list(my_results)
result = my_results[0]
Would the DB get hit twice, or only once? Is what I'm saying too complex for a framework to be able to do... If you derived 'result' from 'all_results[0]' instead of 'my_results[0]', then it wouldn't have to hit the DB twice correct?


Edit: Wait, I think I'm getting it, the reason is that 'my_results' is a model object. As soon as that model is dumped into another variable then the model object needs to contain data. So, it's not as if Django looks ahead to find out what all you are going to be using entirely before hitting the DB.

Nolgthorn fucked around with this message at 16:58 on Mar 7, 2008

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
My psychic nerd head exploder beams are in full effect!

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I am trying to extend the User model and implement django-profiles so that my users can CRUD their own accounts at will.

Update: I found the error was in AUTH_PROFILE_MODULE, I was referencing my project instead of the application that has UserProfile defined in it.

Edit: nevermind

Nolgthorn fucked around with this message at 17:40 on Mar 8, 2008

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Is there a good way of creating "HTML helpers", in the way that Rails does it, so that I could type {% paginnnnnnnnnnnator %} and have that just read from another template? Do I just need to include it on the page then and what does the code look like to do that?

What if I'm calling it multiple times in the same page, is there a way to do it properly with a minimal of code and duplication?

Can I pass arguments?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
What are you going to do in order to ensure that two of the same slugs aren't generated on the same day?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Learning to use Django is hard, can you recommend me a book to read apart from 'The Django Book'?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Upon further reading, it seems the documentation (which I was reading before) when coupled with 'The Django Book' make for a good amount of well written documentation. I think I'm starting to understand what's going on.

The confusion came from how different the scope is here, Rails really does tie you down and make things difficult in a lot of respects. Django only loads what it is using on each page? Rails doesn't require you to load/initialize/import anything at all, I suspect that makes each Rails app somewhat resource intensive in comparison.

I see other benefits to the way Django is doing it, I'm just surprised. That amount of control and manual 'work' is unheard of in the Rails community.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Most notably chapters 8 and 9 of the Django Book are being really helpful, these are answering a lot of questions I had which were slowing down my learning process. Things like "Why are some of these in quotes and others not?" or "Why do some methods need to be explicitly imported and others are just magically working, how does it do that?"

I generally had this feeling like I needed an instructor or a book to explain what I wasn't getting. I am loving this now, those new to this should be sure and read those chapters.

Hah! I'm building my personal project in Django.
It's going to be a content driven comedy web site updated and moderated by it's users, move over sa. Hahahaha!

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I've made one or two custom template filters which I would like to use on every or almost every single page of the web site.

I have a utils application, in it I've created a templatetags folder and added my_filters.py in there. How can I have access to the methods in my_filters without loading it from every template?

That would be easier.

Currently, I always have to put {% load my_filters %} to the top of each one.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Cool, in order to get it to work I had to use something like this but it works.
code:
# In __init__.py
from django.template import add_to_builtins
add_to_builtins('utils.templatetags.my_filters')
This raises the question about how liberally imports should or should not be used. How much extra processing is required when you use urls.py and views.py to import a lot of stuff, instead of just directing everything in your urls.py file to you view, to only import there.

I would be concerned, that if importing does use up resources, then more resources are being used than necessary. Thoughts?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Honestly REST has no place in Django, it is by far the worst feature ever added to Rails... what is the point of it, it's huge, it's obtrusive to coding anything, it offers negligible benefit to users.

In fact it's worse for users if it is implemented incorrectly as it was initially with Rails, with the funky semicolons in the URL.

Is there any reason at all that we would need REST in Django? I understand you don't have to use REST in Rails but when you don't, you cannot use a lot of the new or upcoming features. It's like development in RESTless rails just stopped dead after the idea popped up and it's unnecessary. :mad:

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I am hoping I can find some help, currently I am trying to build a page which will create a new object, save it to the database and redirect me to the new object. The object being created spans over two tables, this is the first time I've done this so I want to be sure I'm doing it correctly before I continue doing it wrong for the rest of the project.

The object is named Conversation and it is basically a thread topic with first post. CommentContainer is the post, Comment is going to contain previous edits but I'm not concerned about it too much right now.

EDIT: I've updated my code into working status.

urls.py
code:
from django.conf.urls.defaults import *
from conversation import views

urlpatterns = patterns('',
    url(r'^$', views.conversation_list, name='conversations_conversation_list'),
    #url(r'^show/(?P<id>\d+)/$', list_detail.object_detail,
    #    queryset=Conversation.objects.all(), name='conversations_conversation'),
    url(r'^create/$', views.create_conversation, name='conversations_conversation_create'),
    #url(r'^edit/$', views.edit_profile, name='conversations_conversation_edit'),
)
views.py
code:
from django.views.generic.list_detail import object_list
from django.views.generic.create_update import create_object
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from conversation import models, forms

def conversation_list(request):
    return object_list(request, queryset=models.Conversation.objects.all(), paginate_by=10)

def create_conversation(request):
    if request.method == "POST":
        conversation_form = forms.ConversationForm(request.POST, instance=models.Conversation())
        comment_form = forms.CommentContainerForm(request.POST, instance=models.CommentContainer())
        if conversation_form.is_valid() and comment_form.is_valid():
            new_conversation = conversation_form.save()
            new_comment = comment_form.save(commit=False)
            new_comment.conversation = new_conversation
            new_comment.save()
            return HttpResponseRedirect('/coffee/')
    else:
        conversation_form = forms.ConversationForm(instance=models.Conversation())
        comment_form = forms.CommentContainerForm(instance=models.CommentContainer())
    return render_to_response('conversation/create_conversation.html',
            {'conversation_form': conversation_form, 'comment_form': comment_form})
models.py
code:
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Conversation(models.Model):
    # These three fields are a polymorphic association
    content_type = models.ForeignKey(ContentType, null=True)
    object_id = models.PositiveIntegerField(null=True)
    # Automatically sets the above two values given a valid Model object
    content_object = generic.GenericForeignKey()
    
    user = models.ForeignKey(User, editable=False, null=True)
    title = models.CharField(max_length=100)
    subtitle = models.CharField(max_length=100, blank=True)
    updated_on = models.DateTimeField(auto_now=True, editable=False)
    comments_count = models.PositiveIntegerField(default=0, editable=False)
    class Meta:
        ordering = ["updated_on"]

class CommentContainer(models.Model):
    conversation = models.ForeignKey(Conversation, editable=False)
    user = models.ForeignKey(User, editable=False, null=True)
    message = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_on = models.DateTimeField(auto_now=True, editable=False)
    previous_edits = models.PositiveIntegerField(default=0, editable=False)
    class Meta:
        ordering = ["created_at"]

class Comment(models.Model):
    comment_container = models.ForeignKey(CommentContainer, editable=False)
    user = models.ForeignKey(User, editable=False, null=True)
    message = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    class Meta:
        ordering = ["created_at"]
forms.py
code:
from django import newforms as forms
from conversation import models

class ConversationForm(forms.ModelForm):
    class Meta:
        model = models.Conversation
        fields = ('title', 'subtitle')

class CommentContainerForm(forms.ModelForm):
    class Meta:
        model = models.CommentContainer
        fields = ('message')

class CommentForm(forms.ModelForm):
    class Meta:
        model = models.Comment
        fields = ('message')
Is this the best way to do this?

It seems I was being a little bit greedy and that Generic views don't really do what I wanted, in that the view needed to save two models at once. If I could get it any shorter at all that would be great, In Rails I can fit it into about 5 or 10 lines of code which is why it feels like I'm not doing this right.

If I refresh the page after I've posted Firefox asks me if I want to resend the data, I don't want it to give me the opportunity to do this.

Nolgthorn fucked around with this message at 11:52 on May 27, 2008

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

No Safe Word posted:

James Bennett has apparently just finished up some of the final edits on his upcoming Django book, "Practical Django Projects". You may recognize him from some online sites as ubernostrum, but he's one of the higher profile Django devs and an all-around pretty bright guy. I'm sure his book will be very good, and for anyone looking to get started in the next few months it sounds like this may be a good book to have on the shelf.

But this other book says I could most assuredly learn Django in 24 hours. :colbert: So what's James Bennett going to do about that?

Adbot
ADBOT LOVES YOU

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Neato, is a developers library more or less what you find on the Django web site? I'm curious.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply