标签: django-templates

NameError - 未定义全局名称

我试图从html表单中读取一些数据并将其插入数据库.这样做我坚持这个错误"/ nameEmployee /的NameError:全局名称'get_post_param'未定义"; 我会在这里粘贴我的代码.有人可以帮我解决这个问题.
VIEWS.PY

def createEmployee(request):
    if request.method == "POST":
        userName = get_post_param(request,"userName")
        designation = get_post_param(request,"designation")
        employeeID = get_post_param(request,"employeeID")
        contactNumber = get_post_param(request,"contactNumber")
        project = get_post_param(request,"project")
        dateOfJoin = get_post_param(request,"dateOfJoin")
        EmployeeDetails(userName=userName,designation=designation,employeeID=employeeID,contactNumber=contactNumber,project=project,dateOfJoin=dateOfJoin).save()
        return render_to_response('createEmployee.html')
    else:
        return render_to_response('createEmployee.html')
Run Code Online (Sandbox Code Playgroud)

TEMPLATE.PY

<form action="http://127.0.0.1:8000/createEmployee/" method="POST"> 
Name: <input type="text" name="userName" /><br />
Designation: <input type="text" name="designation" /><br>
EmployeeID: <input type="text" name="employeeID" /><br>
Contact Number: <input type="text" name="contactNumber" /><br>
Project: <input type="text" name="project" /><br>
Date Of Join: <input type="text" name="dateOfJoin" /><br>
<input type="submit" value="Submit" /><br />
</form>
Run Code Online (Sandbox Code Playgroud)

python django django-templates django-models django-views

-1
推荐指数
1
解决办法
8929
查看次数

根据另一个对象列表检查对象列表

我有一个用户配置文件,其中包含一个指向一组收藏对象的manytomany字段.

我有一个queryset返回一组这些类型的对象,独立于收藏夹(虽然它可能包含收藏夹).

我想要做的是:

{% for object in objects %}
    <p>{% if user.get_profile.favorites contains object %} Unfavorite {% else %} Favorite {% endif %}</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

但到目前为止,我找不到一个容易做到这一点的方法.想法?

python django django-templates django-queryset

-1
推荐指数
1
解决办法
65
查看次数

Django模板变量作为列表的关键

我来自这个问题在Django模板中使用变量作为字典键

我在我看来创建了这个上下文:

{'cats': {'LIST1': ['a','b','c'], 'LIST2': ['aa','bb','cc','dd'], 'LIST3': ['f','g']}}
Run Code Online (Sandbox Code Playgroud)

我想要做的是打印列表标题,然后打印所有项目,如

LIST1:
- a
- b
- c 
Run Code Online (Sandbox Code Playgroud)

对于所有列表,所以我在我的模板中这样做了

{% for  l_name, l in cats %}
    {{ l_name }}

    {%for lv in l %}
        {{ lv }}
    {% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

这种打印列表的名称,而不会打印出清单.哪里出错了?

谢谢

django templates django-templates

-1
推荐指数
1
解决办法
165
查看次数

Django:我怎样才能无形地将变量传递给另一个模板?

我的项目中有三个模板 - 我们称之为first.html,second.html,third.html.

first.html使用<input>标记从用户获取字符串:

<input type="radio" name="selection" value="example_string" />
Run Code Online (Sandbox Code Playgroud)

second.html使用显示此字符串{{selection}}.(在我的views.py中,我使用request.POST.get和得到了值render_to_response().)

问题是:如何将此值从second.html发送到third.html?我尝试之一 - 使用<span>标记将信息保存在变量中 - 如下所示,但它似乎不起作用.

<span name="selection" value={{selection}}>{{selection}}</span>
Run Code Online (Sandbox Code Playgroud)

编辑:以下行通过创建虚拟单一单选按钮来工作.我不知道为什么不能创建没有<input>[用户可见]标签的变量.

<input type="radio" name="selected" value={{selected}} checked="checked" />
Run Code Online (Sandbox Code Playgroud)

django django-templates

-1
推荐指数
1
解决办法
141
查看次数

如何使用正则表达式替换所有字符*

我有一个类似的文字

s = bluesky

我想得到它s = *******(等于*没有字符)

我正在寻找python的正则表达式.Plz的帮助.任何帮助将不胜感激

编辑1:

b = '*'*len(s)
Run Code Online (Sandbox Code Playgroud)

我们怎么能在Django模板中做到这一点

html python regex django django-templates

-2
推荐指数
1
解决办法
257
查看次数

“返回渲染(请求,'路径/路径')”如何在 Django 中工作?

请解释“返回渲染(请求,'路径/路径')”如何在 django 中的任何特定 views.py 文件中逐步工作。

我的代码:(views.py)

from django.shortcuts import render
from basic_app.forms import UserForm,UserProfileInfoForm
from . import forms

def index(request):
    return render(request,'basic_app/index.html')

def register(request):
    registered=False
    if request.method=="POST":
        profile_form=UserProfileInfoForm(data=request.POST)
        user_form=UserForm(data=request.POST)
        if profile_form.is_valid() and user_form.is_valid():

            user=user_form.save()
            user.set_password(user.password)
            user.save()

            profile=profile_form.save(commit=False) 
            profile.user=user

            if 'profile_pic' in request.FILES:
                profile.profile_pic=request.FILES['profile_pic']

            profile.save()
            registered=True
        else:
            print(user_form.errors,profile_form.errors)

    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()


    return render(request,'basic_app/registration.html',
                    {'user_form':user_form,
                    'profile_form':profile_form,
                    'registered':registered})
Run Code Online (Sandbox Code Playgroud)

代码:(注册.html)

{% extends "basic_app/basic.html" %}
{% load staticfiles %}
{% block body_block %}

<div class="jumbotron">
  {% if registered %}
  <h1>Thank you …
Run Code Online (Sandbox Code Playgroud)

html python django django-templates

-2
推荐指数
1
解决办法
6236
查看次数

在模板中实现 Django admin TabularInline 表单

我想在 Django 模板中实现像 admin TabularInline 表单这样的表单。(我认为我们需要inlineformset_factory这种表单和 jquery 来动态添加删除按钮。)

这是我的模型:

模型.py

from django.db import models

class Profile(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    created_date = models.DateTimeField(default=timezone.now)

    def get_absolute_url(self):
        return reverse('profile-update', kwargs={'pk': self.pk})

    def __str__(self):
        return f"{self.first_name} {self.last_name}"


class FamilyMember(models.Model):
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    relationship = models.CharField(max_length=100)

    def __str__(self):
        return self.name
Run Code Online (Sandbox Code Playgroud)

如果您能在评论中指导我找到解决方案,我也将不胜感激。

提前致谢。

django django-templates django-forms django-admin

-2
推荐指数
1
解决办法
4425
查看次数

render()缺少1个必需的位置参数:“ template_name”

此屏幕快照中是我的views.py代码的代码。

我收到错误消息:

render() missing 1 required positional argument: 'template_name'
Run Code Online (Sandbox Code Playgroud)

url从运行pycharm

任何想法为什么会这样?

python django django-templates

-3
推荐指数
1
解决办法
1221
查看次数

如何使用django编写代码

如何使用django框架在ubuntu平台上编写python代码.也可以在windows平台上使用django来编写python代码吗?可以将django与eclipse一起使用(我在我的eclipse IDE中插入了PyDev).谢谢.

python eclipse django django-templates django-admin

-8
推荐指数
1
解决办法
134
查看次数