| Name | grimmus |
|---|---|
| Email Address | n/a |
| Posts | 17 |
-
- 2009-10-19 09:59:15
- Re: showing info from 2 models
- Board » Django » Views
-
I solved this thanks
-
- 2009-10-13 08:05:03
- showing info from 2 models
- Board » Django » Views
-
Hi,
I have a small app where you can add links and save your favourite ones.
I have 2 models, Link and SaveLink
Link
class Link(models.Model): user = models.ForeignKey(User) title = models.CharField(max_length=200) link_type = models.ForeignKey(LinkType) link = models.URLField(verify_exists=True) image = models.CharField(max_length=200) slug = models.SlugField( unique_for_date='pub_date', help_text='Automatically built from title' ) body_html = models.TextField(blank=True) body_markdown = models.TextField() pub_date = models.DateTimeField('Date Published') tags = TagField() PUB_STATUS = ( (0, 'Awaiting Approval'), (1, 'Published'), (2, 'Deleted'), ) status = models.IntegerField(choices=PUB_STATUS, default=0) class Meta: ordering = ('-pub_date',) get_latest_by = 'pub_date' verbose_name_plural = 'links' def __unicode__(self): return u'%s' %(self.title) def get_absolute_url(self): return "/links/%s/%s/" %(self.pub_date.strftime("%Y/%b/%d").lower(), self.slug) def get_save_url(self): return "/links/%s/save/" % (self.id) def save(self): self.body_html = markdown.markdown(self.body_markdown, safe_mode = False) super(Link, self).save() def get_previous_published(self): return self.get_previous_by_pub_date(status__exact=1) def get_next_published(self): return self.get_next_by_pub_date(status__exact=1) def get_tags(self): return Tag.objects.get_for_object(self)
SaveLink
class SaveLink(models.Model): user = models.ForeignKey(User) link = models.ForeignKey(Link) save_date = models.DateTimeField(auto_now=True) class Meta: ordering = ('-save_date',) get_latest_by = 'save_date' verbose_name_plural = 'Saved Links' def __unicode__(self): return u'%s by %s on %s' % (self.link,self.user,self.save_date.strftime("%d/%b/%Y"))
I have a user profile view to show what links the user has saved.
def profile(request): links = SaveLink.objects.filter(user=request.user).order_by('-save_date') t = loader.get_template('registration/profile.html') c = RequestContext(request,{ 'links': links, }) return HttpResponse(t.render(c))
I need to get what links the user has saved and then list them but i cannot seem to show the fields from the Link model as i am using the SaveLink model to get the users saved links.
Can i join two objects together so i can use them both in the output ?
I hope i am being clear
Thanks
-
- 2009-09-30 07:48:12
- Re: dynamic sitename in template from settings.py
- Board » Django » Templates
-
ok, thanks
but where would i actually define this function ?
so, once defined it i would update TEMPLATE_CONTEXT_PROCESSORS like 'mysite.get_page_title' ?
And how could i call this function in my base.html with a template tag ?
Thanks again
-
- 2009-09-30 07:29:44
- Re: User login not being remembered on all pages
- Board » Django » Middleware
-
Another question if i may....
After a user has registered on the site how would i log them in automatically ?
def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): new_user = form.save() #login user here somehow ?? return HttpResponseRedirect("/links/") else: form = UserCreationForm() return render_to_response("registration/register.html", { 'form': form, })
-
- 2009-09-30 07:17:07
- Re: User login not being remembered on all pages
- Board » Django » Middleware
-
Thanks alot that worked

-
- 2009-09-30 07:13:43
- Re: User login not being remembered on all pages
- Board » Django » Middleware
-
ok, i changed
c = Context({ 'links': links, })
to
c = RequestContext({ 'links': links, })
is there something else i need to change ?
this is in my settings.py
TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.auth', )
Thanks
-
- 2009-09-30 06:57:52
- User login not being remembered on all pages
- Board » Django » Middleware
-
Hi,
I am using the standard register/login on my site but when a user is logged in some pages say welcme userXX and some other pages always say welcome, new user.
templates/base.html:{% if user.is_authenticated %} <p>Welcome, {{ user.username }}. <a href="/accounts/profile/">Profile</a> | <a href="/accounts/logout/">Logout</a></p> {% else %} <p>Welcome, new user. Please <a href="/accounts/register/">register</a> or <a href="/accounts/login/">login</a></p> {% endif %}
accounts/urls.py:
from django.conf.urls.defaults import * from django.contrib.auth.views import login, logout urlpatterns = patterns('', (r'^login/$', login), (r'^logout/$', logout, {'template_name': 'registration/logout.html'}), (r'^register/$', 'accounts.views.register'), (r'^profile/$', 'accounts.views.profile'), )
The profile page above seems to never realize a user is logged in, then when i go to another url it remembers the user being logged in.
accounts/views.py
from django import forms from django.template import Context, loader from django.contrib.auth.forms import UserCreationForm from django.http import HttpResponse,HttpResponseRedirect from django.contrib.auth.models import User from django.shortcuts import render_to_response from links.models import Link,SaveLink def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): new_user = form.save() #login user aswell return HttpResponseRedirect("/links/") else: form = UserCreationForm() return render_to_response("registration/register.html", { 'form': form, }) def profile(request): links = SaveLink.objects.filter(user=request.user).order_by('-save_date') t = loader.get_template('registration/profile.html') c = Context({ 'links': links, }) return HttpResponse(t.render(c))
Anyone any ideas what i am doing wrong ?
-
- 2009-09-29 07:14:09
- dynamic sitename in template from settings.py
- Board » Django » Templates
-
Hi,
Instead of typing my site title into my html templates i would like it to come from a variable in settings.py
How would i write this value out in my html tempate ?
settings.py:
SITE_NAME = 'Website Inc. - '
base.html:
<title>{{SITE_NAME}} - {%block pagetitle %}{% endblock %}</title>
This isnt working...
Thanks
-
- 2009-07-22 15:20:33
- Setup help on a mac
- Board » Getting Started » Setup and Configuration
-
Hi,
I have django installed but i want to connect it to mysql instead of sqlite.
When i run syncdb it complains about : MySQLdb module: No module named MySQLdb
I tried to download this module but then i get an error : no module named setuptools.
When i try and install this like : sudo apt-get install setuptools (i also tried python-setuptools)
It says couldnt find package setuptools.
Any help is much appreciated. I am a complete noob setting this stuff up on a mac !!
Thanks
-
- 2009-06-30 09:56:40
- Re: Problem referencing media
- Board » Django » Middleware
-
Thanks for the reply
Yep, the file is there.
I get a 404 error : http://www.site.com/site_media/_css/page.css
I am using apache. I dont know how to view the page with the dev server on my production server ?
Could it be something to do with slashes at the start and end of the path ?


