0

我很确定这个问题已经被涵盖了,但是我尝试了我在堆栈中找到的所有相关内容来解决我的问题,但没有任何成功。

这是我的model.py

from django.db import models
from django.contrib.auth.models import User


class Description(models.Model):
    titre_routine = models.CharField(max_length=100)
    type_routine = models.CharField(max_length=100)
    lieu_routine = models.CharField(max_length=100)
    periode = models.CharField(max_length=100, blank=True)
    routine_description = models.TextField()
    raison_chgmt = models.TextField(blank=True)
    user = models.ForeignKey(User)
    #prendre une liste de routine et faire une liste de choix
    #si non ajout a la liste de routine dans nouvelle table

    def __unicode__(self):
        return self.titre_routine

#User.profile = property(lambda u: Description.objects.get_or_create(user=u)[0])


class Rewards(models.Model):
    jour = models.CharField(max_length=4)
    activite_altr = models.TextField()
    first_word = models.CharField(max_length=100, blank=True)
    second_word = models.CharField(max_length=100, blank=True)
    third_word = models.CharField(max_length=100, blank=True)
    urge_validator = models.BooleanField() #urge satisfait ou non?
    urge_validate_date = models.DateField(auto_now=True)
    description = models.ForeignKey(Description)

    def __unicode__(self):
        return self.jour

解释我的模型:一个“用户”可以创建多个“描述”。一个“描述”包含多个“奖励”。...一个“描述”包含多个“其他事物”

这是我的观点.py

#the user access to his dashboard, dashboard filtered by the user logged in.
@login_required(login_url='/userauth/login/')
def dashboard(request):
    routine_info = Description.objects.filter(user=request.user)
    return render_to_response('dashboard.html', {
        'routine_info': routine_info
    })

#dashboard for the reward section that should be like the first dashboard filtered only by the user logged in AND should only show the reward related to One description created previously by the user.
@login_required(login_url='/userauth/login/')
def reward_dashboard(request):
    reward_info = Rewards.objects.all()
    return render_to_response('rewards_dashboard.html', {
        'reward_info': reward_info
    })


#the user can have access to the reward he created as the other def, he should be the only one to have access (again reward created by the user and related to ONE description)
@login_required(login_url='/userauth/login/')
def reward_description(request, reward_id):
    reward_info = Rewards.objects.get(id=reward_id)
    return render_to_response('reward_description.html', {
        'reward': reward_info
    })

#rewardform, I miss the pieces to obtain de description id that will link both table Rewards and Description (and recursively the User table via Description???)
@login_required(login_url='/userauth/login/')
def new_reward(request):

    if request.POST:
        form = RewardsForm(request.POST)

        if form.is_valid():
            obj = form.save(commit= False)
            obj.description = #Missing piece

            #test that could never happen
            if obj.description == 200:
                return HttpResponseRedirect('/reward_dashboard/')
            else:
                obj.save()

            return HttpResponseRedirect('/reward_dashboard/')

    else:
        form = RewardsForm()

    args = {}
    args.update(csrf(request))

    args['form'] = form

    return render_to_response('create_reward.html', args)

概括一下:

- 用户可以访问他创建的奖励,因为他应该是唯一可以访问的人(再次奖励由用户创建并与一个描述相关)

- 奖励部分的仪表板应具有仅由登录用户过滤的第一个仪表板,并且应仅显示先前创建的与奖励相关的一个描述。

-rewardform,我错过了获取de description id的部分,它将链接表Rewards和Description(并通过Description递归地连接User表)

我应该尝试查看会话吗?(当用户创建描述时,我为他分配了一个特定的 ID?)

我只是希望我在解释中清楚地说明了我的情况。

感谢您的帮助!

4

2 回答 2

1

你有多种选择来解决这个问题。这实际上取决于您要在应用程序中实现的工作流程。

在创建奖励之前选择描述

您将有一个页面列出所有用户的描述:这是您的仪表板视图。从这里,用户必须选择他想要添加奖励的描述:通过单击它,通过单击特定的创建奖励链接等。这取决于您(ergo/设计选择)。要点是获取描述IDnew_reward并通过 URLconf( )将其传递给您的视图urls.py,如下所示:

# urls.py
urlpatterns = patterns('',
  # your urlpatterns ...
  url(r'^description/(?P<description_id>\d+)/reward/add/$', 'your.app.views.new_reward', name='add-reward'),
  # your urlpatterns ...
)

这只是一个例子。您可以根据需要设置 URL。无论如何,当您的用户单击链接或按钮(或您选择的任何内容)时,URL 将类似于mondomaine.fr/yourappprefix/description/4/reward/add(其中 4 是所选描述的 id - 它将由您来生成这样的链接)

这样,在您看来,您可以检索描述对象:

#rewardform, I miss the pieces to obtain de description id that will link both table Rewards and Description (and recursively the User table via Description???)
@login_required(login_url='/userauth/login/')
def new_reward(request, description_id):
    description = Description.objects.get_or_404(id=description_id)
    pass

使用直接链接创建奖励

如果这种工作流程在您的情况下甚至不是一个选项,那么只需将描述字段添加到您的RewardsForm. 这样,您的用户将能够瞄准他想要添加奖励的描述。

要将描述列表限制为仅由当前用户创建的描述列表,您可以在您的 views.py 中执行此操作:

# your code ...
else:
    form = RewardsForm()
    form.fields['description'].queryset = Description.objects.filter(user=request.user)

form.is_valid如果返回,也许您将不得不进行保存False

我希望这至少对你有所帮助。

于 2013-08-17T13:43:58.003 回答
0

在我的models.py中,我只添加了一个ForeignKey来链接我的另一个表(Rewards to User),以便能够获取当前登录用户的当前ID(限制不属于该用户的描述)

user = models.ForeignKey(User)   

在我的 urls.py

url(r'^reward_dashboard/(?P<routine_id>\d+)/$', reward_dashboard),
url(r'^reward_create/(?P<description_id>\d+)/$', new_reward),
url(r'^reward_description/(?P<reward_id>\d+)/$', reward_description),

在我的views.py中:

@login_required(login_url='/userauth/login/')
def reward_dashboard(request, routine_id):    (get the routine_id to only get the dashboard related to the description)
    d = Description.objects.get(id=routine_id) #give the object with description related)
    reward_info = Rewards.objects.filter(user=request.user).filter(description=d) #filter with the right description (from the routine_id passed in) and the right user logged.
    return render_to_response('rewards_dashboard.html', {
        'reward_info': reward_info,
        'd' : d      #i will in my template render the d.id in a link {{reward_id}}
    })


@login_required(login_url='/userauth/login/')
def reward_description(request, reward_id):
    reward_info = Rewards.objects.filter(user=request.user).get(id=reward_id)
    return render_to_response('reward_description.html', {
        'reward': reward_info
    })

@login_required(login_url='/userauth/login/')
def new_reward(request, description_id):
    #description = Description.objects.get_or_404(id=description_id)

    d = Description.objects.get(id=description_id) #get all object with the description_id
    if request.POST:
        form = RewardsForm(request.POST)

        if form.is_valid():
            obj = form.save(commit= False)
            obj.description = d #fill the form with the id that come from my template link
            obj.user = request.user #fill the user with the logged one

            if obj.description == 200:
                return HttpResponseRedirect('/dashboard/')
            else:
                obj.save()

            return HttpResponseRedirect("/reward_dashboard/%s" % d.id)

    else:
        form = RewardsForm()



    args = {}
    args.update(csrf(request))

    args['form'] = form
    args['d'] = d 

    return render_to_response('create_reward.html', args)

我希望我的解释会有所帮助。

ps:我会看一下 get_or_404() 以了解它是如何工作的,因为现在我看不到结果的差异,但是重定向尝试访问他不属于的用户肯定会很有趣。

于 2013-08-17T17:34:44.933 回答