我需要做什么才能将管理应用程序移植到前端?
我有一个简单的管理数据库。我想将其移植到搜索和结果页面。我该如何实现这一目标?感谢
编辑乔纳森·芬兰德的评论
from django.db import models
class Lawyer(models.Model):
firm_url = models.CharField('Bio', max_length=200)
firm_name = models.CharField('Firm', max_length=100)
first = models.CharField('First Name', max_length=50)
last = models.CharField('Last Name', max_length=50)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length=300)
class Meta:
ordering = ('last',)
def __unicode__(self):
return self.first
I have a simple admin database. I want to port it to search and results pages. How do I achieve this? Thanks
EDIT re Jonathan Fingland's comment
from django.db import models
class Lawyer(models.Model):
firm_url = models.CharField('Bio', max_length=200)
firm_name = models.CharField('Firm', max_length=100)
first = models.CharField('First Name', max_length=50)
last = models.CharField('Last Name', max_length=50)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length=300)
class Meta:
ordering = ('last',)
def __unicode__(self):
return self.first
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对 Django 有多少经验?如果您有一个仅限管理的站点,那么您可能只真正使用过 Django 模型-视图-模板架构的模型层。我认为您可以通过更彻底地阅读文档和教程来回答您自己的问题(查看 djangobook.com) 。但是,作为入门示例:
对于简单的搜索页面,您想要制作一个其中包含表单的模板。将有一个用于搜索查询的文本框。 “提交”按钮将以某个 url 作为其目标。该 URL 将对应于一个视图函数。视图函数将获取用户输入的文本,执行数据库查询,并最终得到 Lawyer 对象的列表。
至于结果:同样的搜索视图功能将呈现一个模板。它将向其发送一些数据,其中包括(可能除其他外)律师对象列表。然后,在您的结果模板中,您只需循环遍历列表中的所有律师,并以某种方式将它们全部显示在 HTML 中。 (例如:对于每位律师,
)。
我不会给您具体的代码,因为需要编写相当多的代码,这取决于您的实现。这应该会让您了解如何开始...现在去阅读一些文档和示例!我相信你可以用谷歌搜索“django 搜索表单”并找到一个很好的例子。
编辑:这是 Django 书中的一个实际示例它会引导您创建一个搜索页面。
How much experience do you have with Django? If you have an admin-only site, perhaps you've only ever really worked with the model layer of Django's model-view-template architecture. I think you can answer your own question by reading the documentation and tutorials more thoroughly (check out djangobook.com). However, as an example to get you started:
For a simple search page, you want to make a template that has a form in it. There will be a text box for the search query. The "submit" button would have some url as its target. That URL will correspond to a view function. And view function will take the text that the user typed in, perform a database query, and end up with a list of Lawyer objects.
As for the results: this same search view function will render a template. It will send it some data, which will include (possibly among other things), the list of lawyer objects. Then, in your result template, you simply loop through all of the lawyers in your list and display them all somehow in HTML. (eg: for each lawyer,
<li>Last name, first name: Firm</li>
).I'm not giving you specific code, because there is a fair amount of it to write and it will depend on your implementation. This should give you an idea of how to get started... now go read some documentation and examples! I'm sure you can google "django search form" and find a good example.
Edit: Here's an actual example in the Django book that walks you through making a search page.