如何从django url获取变量

时间:2017-07-10 11:23:24

标签: django url

我是Django的新手,想知道如何从网址获取变量。我试过这个:

def employeedetail(request, name):
    return render(request, 'employee/detail.html')

进入此视图

public static void InsertTextBoxAtEachPage()
{
    string filePathIn = MyDir + @"input.docx";
    string filePathOut = MyDir + @"output.docx";

    Document doc = new Document(filePathIn);

    DocumentBuilder builder = new DocumentBuilder(doc);
    LayoutCollector collector = new LayoutCollector(doc);

    int pageIndex = 1;
    foreach (Section section in doc.Sections)
    {
        NodeCollection paragraphs = section.Body.GetChildNodes(NodeType.Paragraph, true);
        foreach (Paragraph para in paragraphs)
        {
            if (collector.GetStartPageIndex(para) == pageIndex)
            {
                builder.MoveToParagraph(paragraphs.IndexOf(para), 0);
                builder.StartBookmark("BM_Page" + pageIndex);
                builder.EndBookmark("BM_Page" + pageIndex);
                pageIndex++;
            }
        }
    }

    collector = new LayoutCollector(doc);
    LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

    const int PageRelativeY = 0;
    const int PageRelativeX = 0;

    foreach (Bookmark bookmark in doc.Range.Bookmarks)
    {
        if (bookmark.Name.StartsWith("BM_"))
        {
            Paragraph para = (Paragraph)bookmark.BookmarkStart.ParentNode;

            Shape textbox = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextBox);

            textbox.Top = PageRelativeY;
            textbox.Left = PageRelativeX;

            int currentPageNumber = collector.GetStartPageIndex(para);

            string barcodeString = string.Format("page {0} of {1}", currentPageNumber, doc.PageCount);
            string barcodeEncodedString = "some barcode string";

            Paragraph paragraph = new Paragraph(doc);
            ParagraphFormat paragraphFormat = paragraph.ParagraphFormat;
            paragraphFormat.Alignment = ParagraphAlignment.Center;
            Aspose.Words.Style paragraphStyle = paragraphFormat.Style;
            Aspose.Words.Font font = paragraphStyle.Font;
            font.Name = "Tahoma";
            font.Size = 12;
            paragraph.AppendChild(new Run(doc, barcodeEncodedString));
            textbox.AppendChild(paragraph);

            paragraph = new Paragraph(doc);
            paragraphFormat = paragraph.ParagraphFormat;
            paragraphFormat.Alignment = ParagraphAlignment.Center;

            paragraphStyle = paragraphFormat.Style;
            font = paragraphStyle.Font;
            font.Name = "Arial";
            font.Size = 10;
            paragraph.AppendChild(new Run(doc, barcodeString));
            textbox.AppendChild(paragraph);

            //Set the width height according to your requirements
            textbox.Width = doc.FirstSection.PageSetup.PageWidth;
            textbox.Height = 50;
            textbox.BehindText = false;

            para.AppendChild(textbox);

            textbox.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
            textbox.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;

            bool isInCell = bookmark.BookmarkStart.GetAncestor(NodeType.Cell) != null;
            if (isInCell)
            {
                var renderObject = collector.GetEntity(bookmark.BookmarkStart);
                layoutEnumerator.Current = renderObject;

                layoutEnumerator.MoveParent(LayoutEntityType.Cell);
                RectangleF location = layoutEnumerator.Rectangle;

                textbox.Top = PageRelativeY - location.Y;
                textbox.Left = PageRelativeX - location.X;
            }
        }
    }

    doc.Save(filePathOut, SaveFormat.Docx);
}

但是我收到一个错误:employeedetail()缺少1个必要的位置参数:' name'

代码是错误的还是我需要以特定方式输入网址?

2 个答案:

答案 0 :(得分:3)

你的正则表达式错了;你没有提供任何匹配的字符。它应该是这样的:

 r'^(?P<name>\w+)/$

匹配所有字母数字字符。

答案 1 :(得分:0)

尝试使用以下解决方案

在你的urls.py

对于url中的字符串参数,请使用以下语法

url(r'^(?P<name>[\w\-]+)/$',employeedetail, name='employeedetail')

这甚至允许字符串在您的网址中传递。

视图中的访问名称参数

def employeedetail(request, name):
    print name
    return render(request, 'employee/detail.html')

希望这会对你有所帮助