Query on generic display views - Django

时间:2017-08-30 20:38:22

标签: django django-models django-templates django-views

For the below url routing for <Connector port="443" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" disableUploadTimeout="true" acceptCount="100" scheme="https" secure="true" SSLEnabled="true" clientAuth="false" sslProtocol="TLS" SSLEnabledProtocols="TLSv1.2" keystoreFile="C:\ProgramData\letsencrypt-win-simple\my.keystore" keystorePass="changeit"/> app,

<input title="Pesquisar" class="botao" onclick="limparVazio()" type="submit" value="Pesquisar">

template <form name="consultarBoletimForm" action="/ptax_internet/consultaBoletim.do?method=consultarBoletim" method="post"> is,

blog

where model for from django.conf.urls import url, include from django.views.generic import ListView, DetailView from blog.models import Post urlpatterns=[ url(r'^$', ListView.as_view( queryset=Post.objects.all().order_by("-date")[:25], template_name="blog/blog.html", ) ) ] app is defined as,

blog.html

MTV of {% extends "personal/header.html" %} {% block content %} {% for post in object_list %} <h5>{{post.date|date:"Y-m-d"}}<a href="/blog/{{post.id}}"> {{post.title}} </a></h5> {% endfor %} {% endblock %} app is structures as,

blog

Question:

class Post(models.Model): title = models.CharField(max_length=140) body = models.TextField() date = models.DateTimeField() def __str__(self): return self.title is internally created as primary key, for every row in the table, but,

What does blog mean in the template(../blog admin.py apps.py __init__.py migrations models.py templates tests.py urls.py views.p )?

2 个答案:

答案 0 :(得分:1)

When you want to go to a particular blog, you need to have a link to that. That's what /blog/{{post.id}} does as a link.

so /blog/1 gives you the first blog. Only that you have to define the url pattern, the view and the template for that.

for student in [lloyd, alice, tyler]:
    print(student["name"], 'result:')
    print(get_average(student))

Then in views:

public class IEnumerableStringReader : TextReader
{
    private readonly IEnumerator<string> _enumerator;

    private bool eof = false; // is set to true when .MoveNext tells us there is no more data.
    private char[] curLine = null;
    private int curLinePos = 0;

    private bool disposed = false;

    public IEnumerableStringReader(IEnumerable<string> input)
    {
        _enumerator = input.GetEnumerator();
    }

    private void GetNextLine()
    {
        if (eof) return;

        eof = !_enumerator.MoveNext();
        if (eof) return;

        curLine = $"{_enumerator.Current}\r\n" // IEnumerable<string> input implies newlines exist betweent he lines.
            .ToCharArray();

        curLinePos = 0;
    }

    public override int Peek()
    {
        if (disposed) throw new ObjectDisposedException("The stream has been disposed.");

        if (curLine == null || curLinePos == curLine.Length) GetNextLine();
        if (eof) return -1;

        return curLine[curLinePos];
    }

    public override int Read()
    {
        if (disposed) throw new ObjectDisposedException("The stream has been disposed.");

        if (curLine == null || curLinePos == curLine.Length) GetNextLine();
        if (eof) return -1;

        return curLine[curLinePos++];
    }

    public override int Read(char[] buffer, int index, int count)
    {
        if (disposed) throw new ObjectDisposedException("The stream has been disposed.");
        if (count == 0) return 0;

        int charsReturned = 0;
        int maxChars = Math.Min(count, buffer.Length - index); // Assuming we dont run out of input chars, we return count characters if we can. If the space left in the buffer is not big enough we return as many as will fit in the buffer. 

        while (charsReturned < maxChars)
        {
            if (curLine == null || curLinePos == curLine.Length) GetNextLine();
            if (eof) return charsReturned;

            int maxCurrentCopy = maxChars - charsReturned;
            int charsAtTheReady = curLine.Length - curLinePos; // chars available in current line                
            int copySize = Math.Min(maxCurrentCopy, charsAtTheReady); // stop at end of buffer.

            // cant use Buffer.BlockCopy because it's byte based and we're dealing with chars.                
            Array.ConstrainedCopy(curLine, curLinePos, buffer, index, copySize);

            index += copySize;
            curLinePos += copySize;
            charsReturned += copySize;
        }

        return charsReturned;
    }

    public override string ReadLine()
    {
        if (curLine == null || curLinePos == curLine.Length) GetNextLine();
        if (eof) return null;

        if (curLinePos > 0) // this is necessary in case the client uses both Read() and ReadLine() calls
        {
            var tmp = new string(curLine, curLinePos, (curLine.Length - curLinePos) - 2); // create a new string from the remainder of the char array. The -2 is because GetNextLine appends a crlf.            
            curLinePos = curLine.Length; // so next call will re-read
            return tmp;
        }

        // read full line.
        curLinePos = curLine.Length; // so next call will re-read
        return _enumerator.Current; // if all the client does is call ReadLine this (faster) code path will be taken.                       
    }

    protected override void Dispose(bool disposing)
    {
        if (!disposed)
        {
            _enumerator.Dispose();
            base.Dispose(disposing);
            disposed = true;
        }
    }
}

And then in templates folder, create a 'blogs/one_blog.html' file. Simplest example being:

using (var tr = new IEnumerableStringReader(input))
using (var reader = new CsvReader(tr))
{
  while (reader.ReadRecord())
  {
    // do whatever
  }
}

Just make sure that you understand the folder structure for templates.

答案 1 :(得分:1)

它只是一个前缀/前缀/ id /。它也有可能/ article / 1 ......它没关系

urls.py

urlPatterns=[
      url(r'^$', ListView.as_view(
                     model=Post,
                     template_name="blog/blog_list.html",
                   )
            )
      url(r'blog/(?P<pk>[\w-]+)/$', DetailView.as_view(
                     model=Post,
                     template_name="blog/blog_detail.html",
                  )
            )

]