简体   繁体   English

如何在django网站页面上编辑内容?

[英]How can i edit the content on my django website pages?

我是django的新手,我从事过一些项目,在所有项目中,我都使用django管理员界面来管理内容,这显然是我模型中的内容。我主要关心的是是否有一个我可以在不更改代码中实际html情况下在“关于”部分或主页上的区域中编辑内容的方法?

You could create a new model that looks something like this: 您可以创建一个新的模型,如下所示:

class HTMLModel(models.Model):
    title = models.CharField()
    content = models.TextField()

Then you can just load the model in your template and print it out: 然后,您可以将模型加载到模板中并打印出来:

<h1>{{ model.title }}</h1>
<p>{{ model.content }}</p>

Now you are able to change the content from the admin interface and it will show the changes on your webpage. 现在,您可以从管理界面更改内容,它将在您的网页上显示更改。

EDIT: On loading the model, I would assume you just create one instance, so you can load it like so: 编辑:在加载模型时,我假设您只是创建一个实例,因此可以像这样加载它:

return render(request, 'about_me.html', {
    model: HTMLModel.objects.get(pk=1)
})

If you have multiple pages, just add a new field to your model which indicates on which page it should be displayed: 如果您有多个页面,只需在模型中添加一个新字段,以指示应该在哪个页面上显示它:

class HTMLModel(models.Model):
    page_name = models.CharField()
    title = models.CharField()
    content = models.TextField()

Then in your views, you just return the model where page_name = the requested name: 然后在您的视图中,只需返回模型,其中page_name =所请求的名称:

model = HTMLModels.objects.get(page_name=request.path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM